The following code snippet will read environment variables in Java.

import java.util.Map;

public class EnvMap {
    public static void main (String[] args) {
        Map<String, String> env = System.getenv();
        for (String envName : env.keySet()) {
            System.out.format("%s=%s%n",
                              envName,
                              env.get(envName));
        }
    }
}

With a String argument, getenv returns the value of the specified variable. If the variable is not defined, getenv returns null.



Published

21 June 2012

Tags