In general you should use System.getProperty()
or System.getEnv()
.
import java.lang.*;
public class SystemDemo {
public static void main(String[] args) {
// prints Java Runtime Version before property set
System.out.print("Previous : ");
System.out.println(System.getProperty("java.runtime.version" ));
System.setProperty("java.runtime.version", "Java Runtime 1.6.0");
// prints Java Runtime Version after property set
System.out.print("New : ");
System.out.println(System.getProperty("java.runtime.version" ));
}
}
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));
}
}
}
If you are using maven there is another option. You can access environment variables from maven by using ${env.VARIABLE_NAME}
.