Each ProcessBuilder
instance manages a collection of process attributes. The {@link #start()} method creates a new{@link Process} instance with those attributes. The {@link #start()} method can be invoked repeatedly from the same instanceto create new subprocesses with identical or related attributes.
Each process builder manages these process attributes:
user.dir
. false
, meaning that the standard output and error output of a subprocess are sent to two separate streams, which can be accessed using the {@link Process#getInputStream()} and {@link Process#getErrorStream()} methods. If the value is set totrue
, the standard error is merged with the standard output. This makes it easier to correlate error messages with the corresponding output. In this case, the merged data can be read from the stream returned by {@link Process#getInputStream()}, while reading from the stream returned by {@link Process#getErrorStream()} will get an immediate end of file.Modifying a process builder's attributes will affect processes subsequently started by that object's {@link #start()} method, butwill never affect previously started processes or the Java process itself.
Most error checking is performed by the {@link #start()} method.It is possible to modify the state of an object so that {@link #start()} will fail. For example, setting the command attribute toan empty list will not throw an exception unless {@link #start()}is invoked.
Note that this class is not synchronized. If multiple threads access a ProcessBuilder
instance concurrently, and at least one of the threads modifies one of the attributes structurally, it must be synchronized externally.
Starting a new process which uses the default working directory and environment is easy:
Process p = new ProcessBuilder("myCommand", "myArg").start();
Here is an example that starts a process with a modified working directory and environment:
ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2"); Map<String, String> env = pb.environment(); env.put("VAR1", "myValue"); env.remove("OTHERVAR"); env.put("VAR2", env.get("VAR1") + "suffix"); pb.directory(new File("myDir")); Process p = pb.start();
To start a process with an explicit set of environment variables, first call {@link java.util.Map#clear() Map.clear()}before adding environment variables. @since 1.5
|
|
|
|
|
|
|
|
|
|
|
|
|
|