如何使用 exec 在 Java 中设置环境变量?
可能的重复:
如何从 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有重载的 exec 方法,您可以在其中包含环境变量数组。例如 exec(字符串命令,字符串[] envp)。
这是在您执行的子进程中设置 env 变量的示例(带有证明):
但是,它在创建的进程的 env 中设置变量,而不是在当前(Java)进程的 env 中设置变量。
类似地,如果您正在使用 exec 任务,然后您可以使用嵌套的
env
元素将环境变量传递给子进程,例如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:
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.这行不通。当您启动一个新进程时,该进程会收到环境的副本。然后它对环境变量所做的任何更改都在该副本中进行,并且在任何时候都不会对调用者可见。
你到底想达到什么目的?
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?
通过运行“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.
它是
null
因为您启动了另一个cmd.exe
:它与您的 Java 应用程序的环境不同(参见 aix 答案)。我不认为 Java 运行时可以更改环境变量:它可以读取它们,但不能更改它们。
如果要更改正在执行的 JVM 中可用的系统属性,请使用 System.setProperty(String key, String value)。
It's
null
because you launch anothercmd.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)
.