Unix (Linux, Mac)
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:
- ProcessImpl is loaded and init is called.
- Init will call the native method to set
parentPathv
based on the current PATH
.
- All spawn types (i.e., fork and exec, posix_spawn, vfork and exec) will eventually flow through the area of code with the above comment.
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()
.
Windows
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()
.