public ProcessBuilder(List<String> command)
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.
command
- the list containing the program and its arguments
public ProcessBuilder(String... command)
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.
command
- a string array containing the program and its arguments
public ProcessBuilder command(List<String> command)
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.
command
- the list containing the program and its arguments
public ProcessBuilder command(String... command)
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.
command
- a string array containing the program and its arguments
public List<String> command()
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 Map<String,String> environment()
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 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(java.lang.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.
SecurityException
- if a security manager exists and its checkPermission
method doesn't allow access to the process environment
Runtime.exec(String[],String[],java.io.File)
, System.getenv()
public File directory()
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)
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.
directory
- the new working directory
public ProcessBuilder redirectInput(ProcessBuilder.Redirect source)
Sets this process builder's standard input source. Subprocesses subsequently started by this object's
start()
method obtain their standard input from this 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.
source
- the new standard input source
IllegalArgumentException
- if the redirect does not correspond to a valid source of data, that is, has type WRITE
or APPEND
public ProcessBuilder redirectOutput(ProcessBuilder.Redirect destination)
Sets this process builder's standard output destination. Subprocesses subsequently started by this object's
start()
method send their standard output to this 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.
destination
- the new standard output destination
IllegalArgumentException
- if the redirect does not correspond to a valid destination of data, that is, has type READ
public ProcessBuilder redirectError(ProcessBuilder.Redirect destination)
Sets this process builder's standard error destination. Subprocesses subsequently started by this object's
start()
method send their standard error to this 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.
destination
- the new standard error destination
IllegalArgumentException
- if the redirect does not correspond to a valid destination of data, that is, has type READ
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))
.
file
- the new standard input source
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))
.
file
- the new standard output destination
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))
.
file
- the new standard error destination
public ProcessBuilder.Redirect redirectInput()
Returns this process builder's standard input source. Subprocesses subsequently started by this object's
start()
method obtain their standard input from this source. The initial value is
Redirect.PIPE
.
public ProcessBuilder.Redirect redirectOutput()
Returns this process builder's standard output destination. Subprocesses subsequently started by this object's
start()
method redirect their standard output to this destination. The initial value is
Redirect.PIPE
.
public ProcessBuilder.Redirect redirectError()
Returns this process builder's standard error destination. Subprocesses subsequently started by this object's
start()
method redirect their standard error to this destination. The initial value is
Redirect.PIPE
.
public ProcessBuilder inheritIO()
Sets the source and destination for subprocess standard I/O to be the same as those of the current Java process.
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
.
redirectErrorStream
property
public ProcessBuilder redirectErrorStream(boolean redirectErrorStream)
Sets this process builder's
redirectErrorStream
property.
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
.
redirectErrorStream
- the new property value
public Process start() throws IOException
Starts a new process using the attributes of this process builder.
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
.
Process
object for managing the subprocess
NullPointerException
- if an element of the command list is null
IndexOutOfBoundsException
- if the command is an empty list (has size 0
)
SecurityException
- if a security manager exists and
checkExec
method doesn't allow creation of the subprocess, orcheckRead
method denies read access to the file, orcheckWrite
method denies write access to the fileUnsupportedOperationException
- If the operating system does not support the creation of processes.
IOException
- if an I/O error occurs
Runtime.exec(String[], String[], java.io.File)
public static List<Process> startPipeline(List<ProcessBuilder> builders) throws IOException
Starts a Process for each ProcessBuilder, creating a pipeline of processes linked by their standard output and standard input streams. The attributes of each ProcessBuilder are used to start the respective process except that as each process is started, its standard output is directed to the standard input of the next. The redirects for standard input of the first process and standard output of the last process are initialized using the redirect settings of the respective ProcessBuilder. All other
ProcessBuilder
redirects should be
Redirect.PIPE
.
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. 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
.
String directory = "/home/duke/src";
ProcessBuilder[] builders = {
new ProcessBuilder("find", directory, "-type", "f"),
new ProcessBuilder("xargs", "grep", "-h", "^import "),
new ProcessBuilder("awk", "{print $2;}"),
new ProcessBuilder("sort", "-u")};
List<Process> processes = ProcessBuilder.startPipeline(
Arrays.asList(builders));
Process last = processes.get(processes.size()-1);
try (InputStream is = last.getInputStream();
Reader isr = new InputStreamReader(is);
BufferedReader r = new BufferedReader(isr)) {
long count = r.lines().count();
}
builders
- a List of ProcessBuilders
List<Process>
es started from the corresponding ProcessBuilder
IllegalArgumentException
- any of the redirects except the standard input of the first builder and the standard output of the last builder are not ProcessBuilder.Redirect.PIPE
.
NullPointerException
- if an element of the command list is null or if an element of the ProcessBuilder list is null or the builders argument is null
IndexOutOfBoundsException
- if the command is an empty list (has size 0
)
SecurityException
- if a security manager exists and
checkExec
method doesn't allow creation of the subprocess, orcheckRead
method denies read access to the file, orcheckWrite
method denies write access to the fileUnsupportedOperationException
- If the operating system does not support the creation of processes
IOException
- if an I/O error occurs
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4