Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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?

like image 560
Destructor Avatar asked Nov 18 '14 10:11

Destructor


People also ask

How do I add a variable to the PATH environment?

To add a path to the PATH environment variableIn the System dialog box, click Advanced system settings. On the Advanced tab of the System Properties dialog box, click Environment Variables. In the System Variables box of the Environment Variables dialog box, scroll to Path and select it.


1 Answers

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<String, String> envs = pb.environment();
        System.out.println(envs.get("Path"));
        envs.put("Path", "C:\\mystuff");
        pb.redirectErrorStream();
        pb.start();

    }

}
like image 92
kingoleg Avatar answered Oct 05 '22 08:10

kingoleg