简体   繁体   English

如何在Windows中的ProcessBuilder java中设置PATH环境变量

[英]How to set PATH environment variable in ProcessBuilder java in windows

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 question and this trick his not helping me in my current project. 我遇到了这个问题 ,这个技巧在我目前的项目中并没有帮助我。 What should I do to get around this? 我该怎么办才能解决这个问题?

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

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM