如何使用 exec 在 Java 中设置环境变量?

发布于 2024-12-22 10:27:50 字数 656 浏览 6 评论 0原文

可能的重复:
如何从 Java 设置环境变量?

尝试设置环境变量,然后读回它以验证它是否确实设置。

我有以下内容:

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);
    }
}

但是,似乎 FOO 始终为 null,这意味着它可能未正确设置。

我的 exec 命令正确吗? javadoc 声明它可以采用字符串参数作为命令。

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

等数载,海棠开 2024-12-29 10:27:50

有重载的 exec 方法,您可以在其中包含环境变量数组。例如 exec(字符串命令,字符串[] envp)

这是在您执行的子进程中设置 env 变量的示例(带有证明):

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);
}

但是,它在创建的进程的 env 中设置变量,而不是在当前(Java)进程的 env 中设置变量。

类似地,如果您正在使用 exec 任务,然后您可以使用嵌套的 env 元素将环境变量传递给子进程,例如

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

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>
沫尐诺 2024-12-29 10:27:50

这行不通。当您启动一个新进程时,该进程会收到环境的副本。然后它对环境变量所做的任何更改都在该副本中进行,并且在任何时候都不会对调用者可见。

你到底想达到什么目的?

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?

謌踐踏愛綪 2024-12-29 10:27:50

通过运行“cmd.exe”,您启动一​​个新进程,该进程接收新的环境变量,但是 java 进程不会以这种方式获取新的环境变量设置。

在 Unix/Windows 中,每个进程都有自己的一组环境变量,并在进程创建期间从其父进程继承环境变量。

System.getenv() 只返回进程启动时设置的环境变量,据我所知,没有办法更改java进程本身的环境变量。

检查该设置是否有效的唯一方法是启动一个小批处理脚本,在其中设置并在一个进程中进行检查。

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.

最近可好 2024-12-29 10:27:50

它是 null 因为您启动了另一个 cmd.exe:它与您的 Java 应用程序的环境不同(参见 aix 答案)。

我不认为 Java 运行时可以更改环境变量:它可以读取它们,但不能更改它们。

如果要更改正在执行的 JVM 中可用的系统属性,请使用 System.setProperty(String key, String value)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文