如何使用exec在Java中设置环境变量? [英] How to set an environment variable in Java using exec?
问题描述
可能重复:
如何从Java设置环境变量?
我正在设置一个环境变量,并将其读回来验证它是否已经设置。
我有以下几点: p>
import java.io.IOException;
public class EnvironmentVariable
{
public static void main(String [] args)throws IOException
{
Runtime.getRuntime() cmd.exe设置FOO = false);
String s = System.getenv(FOO);
System.out.println(s);
}
}
但是,似乎 FOO
总是为空,意味着它可能没有正确设置。
我的exec命令是否正确? javadocs声明它可以使用字符串参数作为命令。
任何想法?
这不行。当您开始新的进程时,该进程将接收环境的一个副本。对环境变量进行的任何更改都会在该副本中进行,并且调用者也不会看到。
您实际想要实现什么? p>
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?
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?
这篇关于如何使用exec在Java中设置环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!