Benjamin Lerman said:
Hi,
I'd like to know if there is a way to write an equivalent function as
System.getenv() in Java 1.4. I don't seem to be able to find a method
that returns all the environment variables, be it their names or a map
with names and values...
I use this static method in one of my utility classes whenever I want to
display all the environment variables. You can adapt it to do what you want.
I'm using Java 1.5 but I'm pretty sure this should work in Java 1.4 if you
modify the line which instantiates the 'set' to this, i.e. delete the angle
brackets and what is within them:
Set set = new TreeSet();
The code displays all of the properties in alphabetical order by key name.
---------------------------------------------------------------------------------------------------------------------
static public void displaySystemProperties() {
/*
* Get a collection of all of the property names in the current JVM.
Use
* a TreeSet so that the list of names will be in alphabetical
order.
*/
Properties sysProps = System.getProperties();
Set<String> set = new TreeSet<String>();
for (Enumeration enum0 = sysProps.propertyNames();
enum0.hasMoreElements()
{
set.add((String) enum0.nextElement());
}
/* Display each system property's key and value. */
System.out.println("\n System Properties: ");
for (Iterator it = set.iterator(); it.hasNext()
{
String key = (String) it.next(); //determine the key name
/* Display the key name and its value. */
System.out.println(" " + key + " = " +
sysProps.getProperty(key));
}
return;
}