In Java 1.5, Sun had decided to "undeprecate" the getEnv() method that provides the functionality to get the value of an environment variable. In addition, Sun has added a new method that allows you to discover all of the environment variables that are defined. This tutorial walks you through using both the old and new methods.
To get the value of a single environment variable, you simply need to call the getEnv("environment variable") method providing the environment variable name that you want to evaluate. The following is a simple example:
String variable = System.getenv("WINDIR");
System.out.println(variable);
In Java 1.5, a new method was introduced to allow you to discover all of the environment variables defined in the system. The following example uses the new method to print all of the environment variables and their values:
Map variables = System.getenv();
Set variableNames = variables.keySet();
Iterator nameIterator = variableNames.iterator();
for (int index = 0; index < variableNames.size(); index++)
{
String name = (String) nameIterator.next();
String value = (String) variables.get(name);
System.out.println(name + "=" + value);
}
discuss this topic to forum
