When I execute a command using ProcessBuilder
, how does it know where to look for that command? Using this hack/trick I've modified my PATH variable (verified by inspecting processBuilder.environment()
) to be bad (empty, working dir, etc) but ProcessBuilder can still execute sort, echo, bash, etc. just fine. How is it doing this?!
Note: My particular development environment is OSX but this code will also run on Red Hat Enterprise Linux.
If you want to take control of finding commands, then, well, take control of finding commands. Don't let
ProcessBuilder
search. Use your own code to find what you want to run, and then put an absolute pathname into the parameter toProcessBuilder
.The documentation says
Which in essence mean that where it looks for programs to execute depends on the particular system and JVM you're running on.
I can't find a complete matrix of JVM / System behaviors, but supposedly it behaves similar to the popular shells of the system (
bash
for *nix andcmd
for windows) i.e. it searches the directories in thePATH
environment variable from left to right and executes the first executable file it finds.