|
FlexDoc/Javadoc 2.0 Demo Java Doc |
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.
The startPipeline method can be invoked to create a pipeline of new processes that send the output of each process directly to the next process. Each process has the attributes of its respective ProcessBuilder.
Each process builder manages these process attributes:
If the value is set to true, then:
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, and redirects standard output and error to be appended to a log file:
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"));
File log = new File("log");
pb.redirectErrorStream(true);
pb.redirectOutput(Redirect.appendTo(log));
Process p = pb.start();
assert pb.redirectInput() == Redirect.PIPE;
assert pb.redirectOutput().file() == log;
assert p.getInputStream().read() == -1;
To start a process with an explicit set of environment variables, first call Map.clear() before adding environment variables.
Unless otherwise noted, passing a null argument to a constructor or method in this class will cause a NullPointerException to be thrown.
Nested Class Summary |
||
abstract static class |
Represents a source of subprocess input or a destination of
subprocess output.
|
Constructor Summary |
||
Constructs a process builder with the specified operating
system program and arguments.
|
||
ProcessBuilder(String... command)
Constructs a process builder with the specified operating
system program and arguments.
|
Method Summary |
||
command()
Returns this process builder's operating system program and
arguments.
|
||
Sets this process builder's operating system program and
arguments.
|
||
Sets this process builder's operating system program and
arguments.
|
||
Returns this process builder's working directory.
|
||
Sets this process builder's working directory.
|
||
Returns a string map view of this process builder's environment.
|
||
Sets the source and destination for subprocess standard I/O
to be the same as those of the current Java process.
|
||
Returns this process builder's standard error destination.
|
||
redirectError(File file)
Sets this process builder's standard error destination to a file.
|
||
redirectError(ProcessBuilder.Redirect destination)
Sets this process builder's standard error destination.
|
||
boolean |
Tells whether this process builder merges standard error and
standard output.
|
|
redirectErrorStream(boolean redirectErrorStream)
Sets this process builder's redirectErrorStream property.
|
||
Returns this process builder's standard input source.
|
||
redirectInput(File file)
Sets this process builder's standard input source to a file.
|
||
redirectInput(ProcessBuilder.Redirect source)
Sets this process builder's standard input source.
|
||
Returns this process builder's standard output destination.
|
||
redirectOutput(File file)
Sets this process builder's standard output destination to a file.
|
||
redirectOutput(ProcessBuilder.Redirect destination)
Sets this process builder's standard output destination.
|
||
start()
Starts a new process using the attributes of this process builder.
|
||
Starts a Process for each ProcessBuilder, creating a pipeline of
processes linked by their standard output and standard input streams.
|
Methods inherited from class java.lang.Object |
public ProcessBuilder |
public ProcessBuilder |
(String... command) |
public ProcessBuilder command |
public ProcessBuilder command |
(String... command) |
() |
() |
The returned object may be modified using ordinary Map operations. These modifications will be visible to subprocesses started via the start() 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(Object) 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.
public File directory |
() |
public ProcessBuilder directory |
(File directory) |
public ProcessBuilder redirectInput |
(ProcessBuilder.Redirect source) |
If the source is Redirect.PIPE (the initial value), then the standard input of a subprocess can be written to using the output stream returned by Process.getOutputStream(). If the source is set to any other value, then Process.getOutputStream() will return a null output stream.
public ProcessBuilder redirectOutput |
(ProcessBuilder.Redirect destination) |
If the destination is Redirect.PIPE (the initial value), then the standard output of a subprocess can be read using the input stream returned by Process.getInputStream(). If the destination is set to any other value, then Process.getInputStream() will return a null input stream.
public ProcessBuilder redirectError |
(ProcessBuilder.Redirect destination) |
If the destination is Redirect.PIPE (the initial value), then the error output of a subprocess can be read using the input stream returned by Process.getErrorStream(). If the destination is set to any other value, then Process.getErrorStream() will return a null input stream.
If the redirectErrorStream attribute has been set true, then the redirection set by this method has no effect.
public ProcessBuilder redirectInput |
(File file) |
This is a convenience method. An invocation of the form redirectInput(file) behaves in exactly the same way as the invocation redirectInput (Redirect.from(file)).
public ProcessBuilder redirectOutput |
(File file) |
This is a convenience method. An invocation of the form redirectOutput(file) behaves in exactly the same way as the invocation redirectOutput (Redirect.to(file)).
public ProcessBuilder redirectError |
(File file) |
This is a convenience method. An invocation of the form redirectError(file) behaves in exactly the same way as the invocation redirectError (Redirect.to(file)).
public ProcessBuilder.Redirect redirectInput |
() |
public ProcessBuilder.Redirect redirectOutput |
() |
public ProcessBuilder.Redirect redirectError |
() |
public ProcessBuilder inheritIO |
() |
This is a convenience method. An invocation of the form
pb.inheritIO()
behaves in exactly the same way as the invocation
pb.redirectInput(Redirect.INHERIT)
.redirectOutput(Redirect.INHERIT)
.redirectError(Redirect.INHERIT)
This gives behavior equivalent to most operating system
command interpreters, or the standard C library function
system().
public boolean redirectErrorStream |
() |
If this property is true, then any error output generated by subprocesses subsequently started by this object's start() method will be merged with the standard output, so that both can be read using the Process.getInputStream() method. This makes it easier to correlate error messages with the corresponding output. The initial value is false.
public ProcessBuilder redirectErrorStream |
(boolean redirectErrorStream) |
If this property is true, then any error output generated by subprocesses subsequently started by this object's start() method will be merged with the standard output, so that both can be read using the Process.getInputStream() method. This makes it easier to correlate error messages with the corresponding output. The initial value is false.
public Process start |
() |
throws |
The new process will invoke the command and arguments given by command(), in a working directory as given by directory(), with a process environment as given by environment().
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.
A minimal set of system dependent environment variables may be required to start a process on some operating systems. As a result, the subprocess may inherit additional environment variable settings beyond those in the process builder's environment().
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.
If the operating system does not support the creation of processes, an UnsupportedOperationException will be thrown.
Subsequent modifications to this process builder will not affect the returned Process.
(List<ProcessBuilder> builders) |
|
throws |
All input and output streams between the intermediate processes are not accessible. The standard input of all processes except the first process are null output streams The standard output of all processes except the last process are null input streams.
The redirectErrorStream() of each ProcessBuilder applies to the respective process. If set to true, the error stream is written to the same stream as standard output.
If starting any of the processes throws an Exception, all processes are forcibly destroyed.
The startPipeline method performs the same checks on each ProcessBuilder as does the start() method. Each new process invokes the command and arguments given by the respective process builder's command(), in a working directory as given by its directory(), with a process environment as given by its environment().
Each process builder's command is checked to be 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.
A minimal set of system dependent environment variables may be required to start a process on some operating systems. As a result, the subprocess may inherit additional environment variable settings beyond those in the process builder's environment().
If there is a security manager, its checkExec method is called with the first component of each process builder'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.
If the operating system does not support the creation of processes, an UnsupportedOperationException will be thrown.
Subsequent modifications to any of the specified builders will not affect the returned Process.
|
FlexDoc/Javadoc 2.0 Demo Java Doc |