Wednesday, June 27, 2007

Getting environment values

There are two ways for getting the environment values in Java:

1. We can use System.getProperty(systemPropertyString) to retrieve the values, such as java.version, java.home, os.name, user.name, user.home, user.dir, java.io.tmpdir etc;

2. Use System.getenv to enumerate all the system environment:

==============================================================
import java.util.*;

public class ListEnv {
public static void listAllEnv(){
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);
}
}

public static void main(String[] args) {
listAllEnv();
}
}
==============================================================

No comments: