$PATH variable isn't inherited through getRunt

2019-01-20 13:24发布

问题:

I'm trying to start a script by the following command in Java:

proc = Runtime.getRuntime().exec(cmd, null, fwrkDir);

The command, typed in a console, works flawlessly. But here it doesn't seem to find the script, even though it's path is added to the $PATH variable. Doesn't Java automatically inherit all such variables, if null is passed as Environment?

回答1:

proc = Runtime.getRuntime().exec(cmd, null, fwrkDir);

should be

proc = Runtime.getRuntime().exec(cmd, "PATH=$PATH:/android-sdk-linux_x86/platform-tools", fwrkDir);


回答2:

Note that the second parameter to the exec() call in your example is null. The second parameter is where you set the environment for the command you are executing. If you are using Java 6, consider using ProcessBuilder.



回答3:

Found the solution myself. Instead of changing the $PATH variable in .bashsrc, I had to change the $PATH variable in /etc/profile by adding

PATH=$PATH:/android-sdk-linux_x86/platform-tools

Does anyone know why Java needs the global change of the path? Thanks for your answers, though!