Java Reference
In-Depth Information
Example 4.2 Java program to dump environment variables
/*
* simple environment examiner
*/
import java.util.*;
public class
AllEnv
{
public static void
main(String [] args)
{
Properties props = java.lang.System.getProperties();
for (Enumeration enm = props.propertyNames(); enm.hasMoreElements();)
{
String key = (String) enm.nextElement();
System.out.print(key);
System.out.print(" = ");
System.out.println(props.getProperty(key));
}
} // main
} // class AllEnv
had a getenv() method. Its argument was a String name of an environment
variable and it returned the environment variable's value as a String . This has
been deprecated. In fact, an attempt to use getenv() in more recent versions
of Java will result in an exception. Sun decided that this was too platform-
specific; not all platforms have environment variables.
Now (Java 1.3 and beyond) the preferred approach is to use the
getProperties() and getProperty() methods of the System class. How
are these different from the getenv() approach? To a Linux developer,
getenv() was easy and straightforward—just not very portable. To accommo-
date other systems, Java defines a set of properties that are reasonable to expect
to be defined on any system, and provides a Java property name for each one.
To see the entire list, call the getProperties() method. It returns a
Properties class, which is an extension of the Hashtable class. From this
class you can get an Enumeration of the names, as Example 4.2 demonstrates.
Now compile and run this example:
Search WWH ::




Custom Search