Reputation: 22635
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.
Upvotes: 6
Views: 7905
Reputation: 56
ProcessBuilder likely finds commands through:
(/usr/bin, /bin, etc.)
These factors can allow command execution even with a modified PATH. To determine the exact method, you'd need to use system tracing tools or examine the Java source code for your specific runtime.
Upvotes: 0
Reputation: 461
For UNIX like OSes, the parent's path is searched. Source openjdk childproc.c:
/* We must search PATH (parent's, not child's) */
The flow is as follows:
parentPathv
based on the current PATH
.Which means that any executable without an exact path will be searched for in parentPathv
which came from the PATH
value of the Java process, not the PATH
that was configured as part of processBuilder.environment()
.
For Windows, ProcessImpl will normalize the executable in getExecutablePath and then pass it to the Win32 function CreateProcess which has these rules:
If the file name does not contain a directory path, the system searches for the executable file in the following sequence:
1. The directory from which the application loaded.
2. The current directory for the parent process.
3. The 32-bit Windows system directory. Use the GetSystemDirectory function to get the path of this directory.
4. The 16-bit Windows system directory. There is no function that obtains the path of this directory, but it is searched. The name of this directory is System.
5. The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
6. The directories that are listed in the PATH environment variable. Note that this function does not search the per-application path specified by the App Paths registry key. To include this per-application path in the search sequence, use the ShellExecute function.
CreateProcess also receives the PATH
from the Java process, not what is configured as part of processBuilder.environment()
.
Upvotes: 0
Reputation: 100050
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 to ProcessBuilder
.
Upvotes: 1
Reputation: 420991
The documentation says
[...] a command, a list of strings which signifies the external program file to be invoked and its arguments, if any. Which string lists represent a valid operating system command is system-dependent. [...]
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 and cmd
for windows) i.e. it searches the directories in the PATH
environment variable from left to right and executes the first executable file it finds.
Upvotes: 5