Each ProcessBuilder
instance manages a collection
of process attributes. The
method creates a new
Process
instance with those attributes. The
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:
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
and
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
, while
reading from the stream returned by
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("myDir"); Process p = pb.start();
To start a process with an explicit set of environment variables, first call before adding environment variables.
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.command
array, in the same order. It is not checked whether
command
corresponds to a valid operating system
command.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.command
array, in the same order. It is not
checked whether command
corresponds to a valid
operating system command.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.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.The returned object may be modified using ordinary Map
operations. These modifications will be
visible to subprocesses started via the
method. Two ProcessBuilder
instances always
contain independent process environments, so changes to the
returned map will never be reflected in any other
ProcessBuilder
instance or the values returned by
System.getenv
.
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 NullPointerException . Attempting to query the presence of a key or value which is not of type String will throw a ClassCastException .
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 UnsupportedOperationException or IllegalArgumentException if the modification is not permitted by the operating system.
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 Object#equals and Object#hashCode methods.
The returned map is typically case-sensitive on all platforms.
If a security manager exists, its
checkPermission
method is called with a
RuntimePermission
("getenv.*")
permission. This may result in a SecurityException
being
thrown.
When passing information to a Java subprocess, system properties are generally preferred over environment variables.
The equals
method implements an equivalence relation
on non-null object references:
x
, x.equals(x)
should return
true
.
x
and y
, x.equals(y)
should return true
if and only if
y.equals(x)
returns true
.
x
, y
, and z
, if
x.equals(y)
returns true
and
y.equals(z)
returns true
, then
x.equals(z)
should return true
.
x
and y
, multiple invocations of
x.equals(y) consistently return true
or consistently return false
, provided no
information used in equals
comparisons on the
objects is modified.
x
,
x.equals(null)
should return false
.
The equals method for class Object
implements
the most discriminating possible equivalence relation on objects;
that is, for any non-null reference values x
and
y
, this method returns true
if and only
if x
and y
refer to the same object
(x == y
has the value true
).
Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.
java.util.Hashtable
.
The general contract of hashCode
is:
hashCode
method on each of
the two objects must produce the same integer result.
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
wait
methods.
The awakened thread will not be able to proceed until the current thread relinquishes the lock on this object. The awakened thread will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened thread enjoys no reliable privilege or disadvantage in being the next thread to lock this object.
This method should only be called by a thread that is the owner of this object's monitor. A thread becomes the owner of the object's monitor in one of three ways:
synchronized
statement
that synchronizes on the object.
Class,
by executing a
synchronized static method of that class.
Only one thread at a time can own an object's monitor.
wait
methods.
The awakened threads will not be able to proceed until the current thread relinquishes the lock on this object. The awakened threads will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened threads enjoy no reliable privilege or disadvantage in being the next thread to lock this object.
This method should only be called by a thread that is the owner
of this object's monitor. See the notify
method for a
description of the ways in which a thread can become the owner of
a monitor.
If this property is true
, then any error output
generated by subprocesses subsequently started by this object's
method will be merged with the standard
output, so that both can be read using the
method. This makes it easier
to correlate error messages with the corresponding output.
The initial value is false
.
redirectErrorStream
property.
If this property is true
, then any error output
generated by subprocesses subsequently started by this object's
method will be merged with the standard
output, so that both can be read using the
method. This makes it easier
to correlate error messages with the corresponding output.
The initial value is false
.
The new process will invoke the command and arguments given by , in a working directory as given by , with a process environment as 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
checkExec
method is called with the first component of this object's
command
array as its argument. This may result in
a SecurityException
being thrown.
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 IOException .
Subsequent modifications to this process builder will not affect the returned Process .
toString
method returns a string that
"textually represents" this object. The result should
be a concise but informative representation that is easy for a
person to read.
It is recommended that all subclasses override this method.
The toString
method for class Object
returns a string consisting of the name of the class of which the
object is an instance, the at-sign character `@
', and
the unsigned hexadecimal representation of the hash code of the
object. In other words, this method returns a string equal to the
value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
The current thread must own this object's monitor. The thread
releases ownership of this monitor and waits until another thread
notifies threads waiting on this object's monitor to wake up
either through a call to the notify
method or the
notifyAll
method. The thread then waits until it can
re-obtain ownership of the monitor and resumes execution.
As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:
synchronized (obj) { while (<condition does not hold>) obj.wait(); ... // Perform action appropriate to condition }This method should only be called by a thread that is the owner of this object's monitor. See the
notify
method for a
description of the ways in which a thread can become the owner of
a monitor.The current thread must own this object's monitor.
This method causes the current thread (call it T) to place itself in the wait set for this object and then to relinquish any and all synchronization claims on this object. Thread T becomes disabled for thread scheduling purposes and lies dormant until one of four things happens:
A thread can also wake up without being notified, interrupted, or timing out, a so-called spurious wakeup. While this will rarely occur in practice, applications must guard against it by testing for the condition that should have caused the thread to be awakened, and continuing to wait if the condition is not satisfied. In other words, waits should always occur in loops, like this one:
synchronized (obj) { while (<condition does not hold>) obj.wait(timeout); ... // Perform action appropriate to condition }(For more information on this topic, see Section 3.2.3 in Doug Lea's "Concurrent Programming in Java (Second Edition)" (Addison-Wesley, 2000), or Item 50 in Joshua Bloch's "Effective Java Programming Language Guide" (Addison-Wesley, 2001).
If the current thread is interrupted by another thread while it is waiting, then an InterruptedException is thrown. This exception is not thrown until the lock status of this object has been restored as described above.
Note that the wait method, as it places the current thread into the wait set for this object, unlocks only this object; any other objects on which the current thread may be synchronized remain locked while the thread waits.
This method should only be called by a thread that is the owner
of this object's monitor. See the notify
method for a
description of the ways in which a thread can become the owner of
a monitor.
This method is similar to the wait
method of one
argument, but it allows finer control over the amount of time to
wait for a notification before giving up. The amount of real time,
measured in nanoseconds, is given by:
1000000*timeout+nanos
In all other respects, this method does the same thing as the method of one argument. In particular, wait(0, 0) means the same thing as wait(0).
The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until either of the following two conditions has occurred:
notify
method
or the notifyAll
method.
timeout
milliseconds plus nanos
nanoseconds arguments, has
elapsed.
The thread then waits until it can re-obtain ownership of the monitor and resumes execution.
As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:
synchronized (obj) { while (<condition does not hold>) obj.wait(timeout, nanos); ... // Perform action appropriate to condition }This method should only be called by a thread that is the owner of this object's monitor. See the
notify
method for a
description of the ways in which a thread can become the owner of
a monitor.