How to set an environment variable in Java using exec? [duplicate]
Asked Answered
M

4

9

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?

Matrices answered 22/12, 2011 at 16:55 Comment(0)
B
2

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?

Binominal answered 22/12, 2011 at 16:58 Comment(3)
Is there a way around that? I want to set the environment variable then use it later (in another process kicked off by Ant)Matrices
@James.Elsey: In that case you'll have to set the variable either prior to launching ant, or from within ant itself. You won't be able to change the variable from a subprocess of ant and somehow make it visible to another subprocess of ant.Binominal
@James, if you are kicking off a process from Ant, then you have the opportunity to set environment variables in that process at that point (when you exec it from Ant).Kearney
K
20

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>
Kearney answered 22/12, 2011 at 16:58 Comment(0)
B
2

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?

Binominal answered 22/12, 2011 at 16:58 Comment(3)
Is there a way around that? I want to set the environment variable then use it later (in another process kicked off by Ant)Matrices
@James.Elsey: In that case you'll have to set the variable either prior to launching ant, or from within ant itself. You won't be able to change the variable from a subprocess of ant and somehow make it visible to another subprocess of ant.Binominal
@James, if you are kicking off a process from Ant, then you have the opportunity to set environment variables in that process at that point (when you exec it from Ant).Kearney
T
1

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 an 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).

Thaine answered 22/12, 2011 at 17:0 Comment(1)
the setProperty will only create a property on the level of the current instance of you JVM. It will not be available for other applications in any way.Hiett
O
1

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.

Oligoclase answered 22/12, 2011 at 17:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.