| |
| [ Blog ] [ Open Source ] [ Programming ] | |
« Java5 (JDK 1.5) Released | Main | Getting the Environment in JDK 1.5 (update) »
JDK and 1.1 used to allow people to use java.lang.System.getenv(String) to get the value of one environment variable. This was really cool, but you never could get the entire contents of the environment. Then with JDK 1.2, Sun deprecated this method and even intentionally made it not function anymore since it was not cross-platform.
Now for the good news; In Java 5.0 (JDK 1.5) Sun as un-deprecated System.getenv(String) and even added System.getenv() that returns a Map of Strings. Way cool in my opinion, and very useful for those of us writing Java code trying to solve real problems.
Here's a code sample showing how this works:
package oracle.jdeveloper.test.tiger;
import java.util.Iterator;
import java.util.Map;
public class EnvironmentTest
{
public static void main(String[] args)
{
Map<String, String> environment = System.getenv();
Iterator<Map.Entry<String,String>> iter = environment.entrySet().iterator();
while( iter.hasNext() )
{
Map.Entry<String,String> entry = iter.next();
String key = entry.getKey();
String value = entry.getValue();
System.out.println( "[" + key + " = " + value + "]");
}
}
}
Posted by rcleveng at September 30, 2004 11:46 AM