java 如何在Windows中的ProcessBuilder java中设置PATH环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26992165/
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
How to set PATH environment variable in ProcessBuilder java in windows
提问by Destructor
I am trying to set the PATH environment variable for the process builder in java, I tried the following:
我正在尝试为 java 中的进程构建器设置 PATH 环境变量,我尝试了以下操作:
ProcessBuilder pb = new ProcessBuilder(command);
Map<String, String> mp = pb.environment();
mp.put("Path", "myPath");
pb.start();
But the following did not work, the process builder picked the default system path. I came across this questionand this trick his not helping me in my current project. What should I do to get around this?
但是以下不起作用,流程构建器选择了默认系统路径。我遇到了这个问题,这个技巧在我当前的项目中没有帮助我。我该怎么做才能解决这个问题?
回答by kingoleg
Path is used in a new proccess. It doesn't used to find your command.
路径用于新进程。它不用于查找您的命令。
You can try the next solution. Run cmd.exe (bash etc.) and then run your command.
您可以尝试下一个解决方案。运行 cmd.exe(bash 等),然后运行您的命令。
Example:
例子:
public class Test {
public static void main(String[] args) throws IOException {
ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/C", "start", "mystuff.exe");
Map<String, String> envs = pb.environment();
System.out.println(envs.get("Path"));
envs.put("Path", "C:\mystuff");
pb.redirectErrorStream();
pb.start();
}
}