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>
source share