Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enumerate all environment variable in Java

System.getenv(name) needs the name of environment variable.

I am trying to call Runtime.exec(String[], String[], File), the secondary parameter need an array of environment variable, I am not sure whether subprocess will inherit environment variables from current process if I specified this parameter.

For example, if I pass new String[]{"NEWDIR=/home"} as secondary parameter and current java process has environment OLDDIR=/var, what is the return value of System.getenv("OLDDIR") in the subprocess?

updated: Sorry, I have to use Java 1.4 and it seems that System.getenv() was introduced in 1.5?

like image 237
sevenever Avatar asked Sep 15 '11 07:09

sevenever


People also ask

How do I get a list of all environment variables?

To list all the environment variables, use the command " env " (or " printenv "). You could also use " set " to list all the variables, including all local variables.

How do I see my environment variables?

On the Windows taskbar, right-click the Windows icon and select System. In the Settings window, under Related Settings, click Advanced system settings. On the Advanced tab, click Environment Variables.

What are the Java environment variables?

Like properties in the Java platform, environment variables are key/value pairs, where both the key and the value are strings. The conventions for setting and using environment variables vary between operating systems, and also between command line interpreters.

Which method is used to obtain the values of various environment variables in Java?

The System class in Java provides a method named System. getenv() which can be used to get the value of an environment variable set in the current system.


1 Answers

Map<String, String> env = System.getenv(); for (String envName : env.keySet()) {     System.out.format("%s=%s%n", envName, env.get(envName)); } 
like image 176
stacker Avatar answered Sep 22 '22 18:09

stacker