How to set an environment variable in Java using e

2020-02-01 02:35发布

问题:

This question already has answers here:
Closed 8 years ago.

Possible Duplicate:
How do I set environment variables from Java?

I'm trying to set an environment variable, and read it back to verify it was actually set.

I've got 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 appears that FOO is always null, meaning its probably not set correctly.

Do I have the exec command correct? The javadocs state it can take a string argument as the command.

Any ideas?

回答1:

This won't work. When you start a new process, that process receives a copy of the environment. Any changes it then makes to environment variables are made within that copy, and at no point will become visible to the caller.

What are you actually trying to achieve?



回答2:

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

Here's an example (with proof) of setting an env variable in a child process you exec:

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, that sets the variable in the env of the created process, not of your current (Java) process.

Similarly, if you are creating a process from Ant (as you mention in comments to aix) using the exec task, then you can pass environment variables to the child process using nested env elements, e.g.

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


回答3:

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

In Unix/Windows, each process has it's own set of environment variables and inherits the environment variables from it's parent during process creation.

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

The only way you can check if the set works is by starting a small batch script where you set and do the check in one process.



回答4:

It's null because you launch another cmd.exe : it's a different environment from the one of your Java application (cf aix answer).

I don't think the Java runtime can change a environment variable : it can read them, but can't change them.

If you want to change a system property available in your executing JVM, use System.setProperty(String key, String value").