How to set PATH environment variable in ProcessBuilder java in windows

前端 未结 1 1853
我寻月下人不归
我寻月下人不归 2021-01-11 19:06

I am trying to set the PATH environment variable for the process builder in java, I tried the following:

ProcessBuilder pb = new ProcessBuilder(command);
Map         


        
1条回答
  •  星月不相逢
    2021-01-11 20:12

    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.

    Example:

    public class Test {
    
        public static void main(String[] args) throws IOException {
            ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/C", "start", "mystuff.exe");
            Map envs = pb.environment();
            System.out.println(envs.get("Path"));
            envs.put("Path", "C:\\mystuff");
            pb.redirectErrorStream();
            pb.start();
    
        }
    
    }
    

    0 讨论(0)
提交回复
热议问题