How to set environment variable in java using exec?

Possible duplicate:
How to set environment variables from Java?

I am trying to set an environment variable and read it to make sure that it is actually set.

I have the following:

import java.io.IOException; public class EnvironmentVariable { public static void main(String[] args) throws IOException { Runtime.getRuntime().exec("cmd.exe set FOO=false"); String s = System.getenv("FOO"); System.out.println(s); } } 

However, it looks like FOO always null, which means it probably isn't installed correctly.

Do I have an exec command right? In javadocs state, it can take a string argument as a command.

Any ideas?

+7
source share
4 answers

This will not work. When you start a new process, this process receives a copy of the environment. Any changes that he then makes to the environment variables are made inside this copy and will by no means become visible to the caller.

What are you really trying to achieve?

+3
source

There are overloaded exec methods in which you can include an array of environment variables. For example, exec (command String, String [] envp) .

Here is an example (with proof) of setting the env variable in a child process that you are running:

 public static void main(String[] args) throws IOException { String[] command = { "cmd", "/C", "echo FOO: %FOO%" }; String[] envp = { "FOO=false" }; Process p = Runtime.getRuntime().exec(command, envp); BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); String s = reader.readLine(); System.err.println(s); } 

However, this sets the variable in the env of the created process, and not from your current (Java) process.

Similarly, if you create a process from Ant (as you noted in the comments on aix) using the exec task , you can pass environment variables to the child process using nested env elements, for example

 <exec executable="whatever"> <env key="FOO" value="false"/> </exec> 
+11
source

By running "cmd.exe", you start a new process that receives a new environment variable, however, the java process does not receive this new environment variable in this way.

On Unix / Windows, each process has its own set of environment variables and inherits the environment variables from it by the parent at the time the process was created.

System.getenv () returns only the environment variables that were set when the process started, as far as I can see, there is no way to change the environment variables of the java process itself.

The only way to check if the set is working is to run a small batch of script where you installed and run the check in one process.

+1
source

This value is null because you are running another cmd.exe: it is a different environment from one of your Java applications (cf aix answer).

I do not think that the Java runtime can change the environment variable: it can read them, but cannot change them.

If you want to change the system property available in the executable JVM, use System.setProperty (String key, String value).

0
source

All Articles