Where does Java ProcessBuilder look for command execution?

When I execute a command using ProcessBuilder , how does it know where to look for this command? Using this hack / trick , I changed my PATH variable (verified by checking processBuilder.environment() ) to be bad (empty, working directory, etc.), but ProcessBuilder can still do sort, echo, bash, etc. .d. just fine. How it's done?!

Note. My special development environment is OSX, but this code will also work on Red Hat Enterprise Linux.

+4
source share
2 answers

The documentation states

[...] command, a list of lines that designates the external executable file of the program, and its arguments, if any. Which string lists represent a valid operating system command depends on the system. [...]

Which essentially means that where it searches for executable programs depends on the specific system and JVM you are working on.

I canโ€™t find the full JVM / System behavior matrix, but, presumably, it behaves similarly to the popular shells of the system ( bash for * nix and cmd for windows), that is, it looks from the directories in PATH for the environment variable from left to right and executes the first executable file, which he finds.

+5
source

If you want to take control of your search teams, then, well, take control of your search teams. Do not let ProcessBuilder search. Use your own code to find what you want to run, and then set the absolute path to the ProcessBuilder parameter.

+1
source

Source: https://habr.com/ru/post/1397502/


All Articles