| public final class java.lang ProcessBuilder
|
Java SE 6 |
Each ProcessBuilder instance manages a collection
of process attributes. The #start() method creates a new
Process instance with those attributes. The #start() method can be invoked repeatedly from the same instance
to create new subprocesses with identical or related attributes.
Each process builder manages these process attributes:
System#getenv()).
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 Process#getInputStream() and Process#getErrorStream() methods. If the value is set to
true, 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 Process#getInputStream(), while
reading from the stream returned by Process#getErrorStream() will get an immediate end of file.
Modifying a process builder's attributes will affect processes
subsequently started by that object's #start() method, but
will never affect previously started processes or the Java process
itself.
Most error checking is performed by the #start() method.
It is possible to modify the state of an object so that #start() will fail. For example, setting the command attribute to
an empty list will not throw an exception unless #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 Map.clear()
before adding environment variables.
| since | 1.5 |
| Constructors | |||||
|---|---|---|---|---|---|
| public | ProcessBuilder(List command) Details
Constructs a process builder with the specified operating
system program and arguments. This constructor does not
make a copy of the command list. Subsequent
updates to the list will be reflected in the state of the
process builder. It is not checked whether
command corresponds to a valid operating system
command.
| ||||
| public | ProcessBuilder(String[] command) Details
Constructs a process builder with the specified operating
system program and arguments. This is a convenience
constructor that sets the process builder's command to a string
list containing the same strings as the command
array, in the same order. It is not checked whether
command corresponds to a valid operating system
command.
| ||||
| Methods | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| public ProcessBuilder | command(List command) Details
Sets this process builder's operating system program and
arguments. This method does not make a copy of the
command list. Subsequent updates to the list will
be reflected in the state of the process builder. It is not
checked whether command corresponds to a valid
operating system command.
| ||||||||||||
| public ProcessBuilder | command(String[] command) Details
Sets this process builder's operating system program and
arguments. This is a convenience method that sets the command
to a string list containing the same strings as the
command array, in the same order. It is not
checked whether command corresponds to a valid
operating system command.
| ||||||||||||
| public List | command() Details
Returns this process builder's operating system program and
arguments. The returned list is not a copy. Subsequent
updates to the list will be reflected in the state of this
process builder.
| ||||||||||||
| public File | directory() Details
Returns this process builder's working directory.
Subprocesses subsequently started by this object's #start() method will use this as their working directory.
The returned value may be null -- this means to use
the working directory of the current Java process, usually the
directory named by the system property user.dir,
as the working directory of the child process.
| ||||||||||||
| public ProcessBuilder | directory(File directory) Details
Sets this process builder's working directory.
Subprocesses subsequently started by this object's #start() method will use this as their working directory.
The argument may be null -- this means to use the
working directory of the current Java process, usually the
directory named by the system property user.dir,
as the working directory of the child process.
| ||||||||||||
| public Map | environment() Details
Returns a string map view of this process builder's environment.
Whenever a process builder is created, the environment is
initialized to a copy of the current process environment (see
System#getenv()). Subprocesses subsequently started by
this object's #start() method will use this map as
their environment.
The returned object may be modified using ordinary If the system does not support environment variables, an empty map is returned. The returned map does not permit null keys or values.
Attempting to insert or query the presence of a null key or
value will throw a The behavior of the returned map is system-dependent. A
system may not allow modifications to environment variables or
may forbid certain variable names or values. For this reason,
attempts to modify the map may fail with
Since the external format of environment variable names and values is system-dependent, there may not be a one-to-one mapping between them and Java's Unicode strings. Nevertheless, the map is implemented in such a way that environment variables which are not modified by Java code will have an unmodified native representation in the subprocess. The returned map and its collection views may not obey the
general contract of the The returned map is typically case-sensitive on all platforms. If a security manager exists, its
When passing information to a Java subprocess, system properties are generally preferred over environment variables.
| ||||||||||||
| public boolean | redirectErrorStream() Details
Tells whether this process builder merges standard error and
standard output.
If this property is
| ||||||||||||
| public ProcessBuilder | redirectErrorStream(boolean redirectErrorStream) Details
Sets this process builder's redirectErrorStream property.
If this property is
| ||||||||||||
| public Process | start() throws IOException Details
Starts a new process using the attributes of this process builder.
The new process will
invoke the command and arguments given by This method checks that the command is a valid operating system command. Which commands are valid is system-dependent, but at the very least the command must be a non-empty list of non-null strings. If there is a security manager, its
Starting an operating system process is highly system-dependent. Among the many things that can go wrong are:
In such cases an exception will be thrown. The exact nature
of the exception is system-dependent, but it will always be a
subclass of Subsequent modifications to this process builder will not
affect the returned
| ||||||||||||
| About DocWeb · Bundles · Export · Export All | Top 10 · Statistics · Login |
| About Sun · Contact · Privacy · Terms of Use · Trademarks | Java SE 6 · Copyright © 1994-2009 Sun Microsystems, Inc.All rights reserved. Use is subject to license terms |
![]() |
![]() |
|