在 Java 中在运行时设置 Windows PATH 环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2741175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
set windows PATH environment variable at runtime in Java
提问by hhhh
I have a java program that fires off an executable using the Runtime.exec() method. I'm using the variant that takes in a set of command line params as one argument, and some environment variables as another argument.
我有一个 Java 程序,它使用 Runtime.exec() 方法触发一个可执行文件。我使用的变体将一组命令行参数作为一个参数,将一些环境变量作为另一个参数。
The environment variable I'm tryign to set is path, so i'm passing in "PATH=C:\some\path". This does not work. Is there some trick to this or any alternatives. I am stuck to Java 1.4 unfortunately.
我要设置的环境变量是路径,所以我传入“PATH=C:\some\path”。这不起作用。是否有一些技巧或任何替代方法。不幸的是,我坚持使用 Java 1.4。
回答by Ray Tayek
use http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html#getenv%28java.lang.String%29to get the environment and fix it up then use a flavour of [http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html#exec%28java.lang.String,%20java.lang.String[],%20java.io.File%29][1]to do the exec.
使用http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html#getenv%28java.lang.String%29获取环境并修复它然后使用一种风格[ http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html#exec%28java.lang.String,%20java.lang.String[],%20java. io.File%29][1]执行 exec。
this works with a batch file that has path in it.
这适用于其中包含路径的批处理文件。
package p;
import java.util.*;
public class Run {
static String[] mapToStringArray(Map<String, String> map) {
final String[] strings = new String[map.size()];
int i = 0;
for (Map.Entry<String, String> e : map.entrySet()) {
strings[i] = e.getKey() + '=' + e.getValue();
i++;
}
return strings;
}
public static void main(String[] arguments) throws Exception {
final Map<String, String> env = new HashMap<String, String>(System.getenv());
env.put("Path", env.get("Path") + ";foo");
final String[] strings=mapToStringArray(env);
Runtime.getRuntime().exec("cmd /C start foo.bat",strings);
}
}
回答by Kitsune
If "PATH=C:\some\path" appears in your source code, it would be incorrect as it would be trying to escape the 's' and 'p' in that string, you'd use "PATH=C:\\some\\path" instead (escaping the slashes). Also, you don't want to pass it in as a string directly, but as an arrayof strings (likely with that as the only string in it).
如果“PATH=C:\some\path”出现在你的源代码中,它会是不正确的,因为它会试图转义字符串中的“s”和“p”,你会使用“PATH=C:\ \some\\path" 代替(转义斜线)。此外,您不想直接将其作为字符串传递,而是作为字符串数组(可能是其中唯一的字符串)传递。
回答by user435832
If you want to change the Path variable on windows, you should take a look at JNI_Registry: http://www.trustice.com/java/jnireg/
如果你想改变 Windows 上的 Path 变量,你应该看看 JNI_Registry:http: //www.trustice.com/java/jnireg/
It's a Java binding to the Windows Registry API and comes with a very small footprint. I have used it for my current project and it works just fine.
它是与 Windows Registry API 的 Java 绑定,占用空间非常小。我已经将它用于我当前的项目,并且效果很好。
回答by tex
One solution might be to add an additional command to "exec" where you set the path ... as in the example found here: http://www.neowin.net/forum/topic/620450-java-runtimegetruntimeexec-help/
一种解决方案可能是在您设置路径的“exec”中添加一个额外的命令......如在此处找到的示例:http: //www.neowin.net/forum/topic/620450-java-runtimegetruntimeexec-help/
excerpt:
摘抄:
cmd = new String[7];
cmd[0] = "cmd";
cmd[1] = "/C";
cmd[2] = "set PATH=C:\Program Files\Java\jdk1.6.0_04\bin";
cmd[3] = "copy " + "\"" +path + "\" +name+ "\"" + " C:\java";
cmd[4] = "chdir C:\java";
cmd[5] = "javac *.java";
cmd[6] = "jar cmf mainClass.txt"+" name"+".jar *.class";
try{
Runtime.getRuntime().exec(cmd);