This section provides a tutorial example on how to access environment variables defined in the operating system using the System.getenv() method.
If you want to access environment variables defined in the operating system,
you can use the System.getenv() method, which returns all environment variables as
a java.util.Map object.
Here is tutorial example program to list all environment variables defined in the operating system:
/**
* SystemEnvironmentVariable.java
* Copyright (c) 2010 by Dr. Herong Yang, herongyang.com
*/
class SystemEnvironmentVariable {
public static void main(String[] a) {
// copying all environment variables into a map
java.util.Map envs = System.getenv();
java.util.Map envs = System.getenv();
java.util.Set keys = envs.keySet();
java.util.Iterator i = keys.iterator();
out.println("Envirionment Variables:");
while (i.hasNext()) {
String k = (String) i.next();
String v = (String) envs.get(k);
out.println(" "+k+" = "+v);
}
}
}
When executed on my Windows XP system with JDK 1.6.0,
I got this result: