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

后端 未结 3 708
花落未央
花落未央 2020-12-11 20: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

相关标签:
3条回答
  • 2020-12-11 20:54
    proc = Runtime.getRuntime().exec(cmd, null, fwrkDir);
    

    should be

    proc = Runtime.getRuntime().exec(cmd, "PATH=$PATH:/android-sdk-linux_x86/platform-tools", fwrkDir);
    
    0 讨论(0)
  • 2020-12-11 21:12

    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.

    0 讨论(0)
  • 2020-12-11 21:14

    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!

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