Search code examples
javapathenvironment-variablesenvironmentprocessbuilder

Java application does not use the right PATH environment variable


I've been struggling to find a reason why my Java application does not use the default PATH environment variable. I need it to launch another program with ProcessBuilder. Right now I get "Cannot run program "..." error=2, No such file or directory", though I can run this program from the terminal. I'm using JDK 1.8, Netbeans 8.1, OS X.

Here's the output of System.out.println(System.getenv("PATH"));: /usr/bin:/bin:/usr/sbin:/sbin

The actual value of PATH in the terminal (using echo $PATH) is much longer and contains the paths to the desired executables.

I found some questions treating on the same subject but none could help me with this.

Any help appreciated!


Solution

  • PATH is created by whatever shell you're running, but ProcessBuilder does notrun within a shell and therefore there isn't a PATH to attach to, to resolve your program names. You can provide an environment to ProcessBuilder, but don't believe it's going to let you find your program that's in the PATH. In a project of mine I had to provide a fully-qualified path.]

    [NOTE: Mileage may vary, I seem to remember having somewhat different results between Windows and *nix, and between different *xix.]