$PATH variable isn't inherited through getRuntime().exec

99封情书 提交于 2019-11-27 07:58:25

问题


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!



来源:https://stackoverflow.com/questions/6984138/path-variable-isnt-inherited-through-getruntime-exec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!