4

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?

MByD
  • 135,866
  • 28
  • 264
  • 277
panmari
  • 3,627
  • 3
  • 28
  • 48

3 Answers3

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

should be

proc = Runtime.getRuntime().exec(cmd, "PATH=$PATH:/android-sdk-linux_x86/platform-tools", fwrkDir);
Tisho
  • 8,320
  • 6
  • 44
  • 52
jim
  • 66
  • 1
  • 1
3

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.

DwB
  • 37,124
  • 11
  • 56
  • 82
  • Yes, and in the API it says, that the standard values are passed, if null is given as parameter there... I'd have no idea how to use ProcessBuilder, but thanks for suggesting it. – panmari Aug 10 '11 at 15:01
1

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!

panmari
  • 3,627
  • 3
  • 28
  • 48