Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does Java's ProcessBuilder look to execute commands?

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.

like image 277
Aaron Silverman Avatar asked Feb 20 '12 21:02

Aaron Silverman


People also ask

What is the use of processbuilder command?

ProcessBuilder (String… command): This constructs a process builder with the specified operating system program and arguments. 1. List command (): This method returns the process builder’s operating system program and arguments. Syntax: public List command (). Returns: this process builder's program and its arguments.

What is processbuilder API in Java?

1. Overview The Process API provides a powerful way to execute operating system commands in Java. However, it has several options that can make it cumbersome to work with. In this tutorial, we'll take a look at how Java alleviates that with the ProcessBuilder API. 2. ProcessBuilder API

How to set the process builder's operating system program and arguments?

ProcessBuilder command (List command): This method sets the process builder’s operating system program and arguments. Syntax: public ProcessBuilder command (List command). Returns: NA. Exception: NullPointerException - if the argument is null. 3. ProcessBuilder directory (File directory): This method sets the process builder’s working directory.

What is the difference between process and starting process in Java?

When run, programs occupy their own processes. These are at the level of the operating system. And one process, in a Java program, cannot contain another one. Starting processes. But with ProcessBuilder, in java.lang.ProcessBuilder, we construct and invoke operating system commands.


1 Answers

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.

like image 143
aioobe Avatar answered Nov 15 '22 03:11

aioobe