java.lang
Class ProcessBuilder

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.lang.ProcessBuilder

public final class ProcessBuilder
extends Object sample code for java.lang.Object definition code for java.lang.Object

This class is used to create operating system processes.

Each ProcessBuilder instance manages a collection of process attributes. The start() sample code for java.lang.ProcessBuilder.start() definition code for java.lang.ProcessBuilder.start() method creates a new Process sample code for java.lang.Process definition code for java.lang.Process instance with those attributes. The start() sample code for java.lang.ProcessBuilder.start() definition code for java.lang.ProcessBuilder.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:

Modifying a process builder's attributes will affect processes subsequently started by that object's start() sample code for java.lang.ProcessBuilder.start() definition code for java.lang.ProcessBuilder.start() method, but will never affect previously started processes or the Java process itself.

Most error checking is performed by the start() sample code for java.lang.ProcessBuilder.start() definition code for java.lang.ProcessBuilder.start() method. It is possible to modify the state of an object so that start() sample code for java.lang.ProcessBuilder.start() definition code for java.lang.ProcessBuilder.start() will fail. For example, setting the command attribute to an empty list will not throw an exception unless start() sample code for java.lang.ProcessBuilder.start() definition code for java.lang.ProcessBuilder.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 Map.clear() sample code for java.util.Map.clear() definition code for java.util.Map.clear() before adding environment variables.

Since:
1.5

Constructor Summary
ProcessBuilder sample code for java.lang.ProcessBuilder.ProcessBuilder(java.util.List) definition code for java.lang.ProcessBuilder.ProcessBuilder(java.util.List) (List sample code for java.util.List definition code for java.util.List <String sample code for java.lang.String definition code for java.lang.String > command)
          Constructs a process builder with the specified operating system program and arguments.
ProcessBuilder sample code for java.lang.ProcessBuilder.ProcessBuilder(java.lang.String...) definition code for java.lang.ProcessBuilder.ProcessBuilder(java.lang.String...) (String sample code for java.lang.String definition code for java.lang.String ... command)
          Constructs a process builder with the specified operating system program and arguments.
 
Method Summary
 List sample code for java.util.List definition code for java.util.List <String sample code for java.lang.String definition code for java.lang.String > command sample code for java.lang.ProcessBuilder.command() definition code for java.lang.ProcessBuilder.command() ()
          Returns this process builder's operating system program and arguments.
 ProcessBuilder sample code for java.lang.ProcessBuilder definition code for java.lang.ProcessBuilder command sample code for java.lang.ProcessBuilder.command(java.util.List) definition code for java.lang.ProcessBuilder.command(java.util.List) (List sample code for java.util.List definition code for java.util.List <String sample code for java.lang.String definition code for java.lang.String > command)
          Sets this process builder's operating system program and arguments.
 ProcessBuilder sample code for java.lang.ProcessBuilder definition code for java.lang.ProcessBuilder command sample code for java.lang.ProcessBuilder.command(java.lang.String...) definition code for java.lang.ProcessBuilder.command(java.lang.String...) (String sample code for java.lang.String definition code for java.lang.String ... command)
          Sets this process builder's operating system program and arguments.
 File sample code for java.io.File definition code for java.io.File directory sample code for java.lang.ProcessBuilder.directory() definition code for java.lang.ProcessBuilder.directory() ()
          Returns this process builder's working directory.
 ProcessBuilder sample code for java.lang.ProcessBuilder definition code for java.lang.ProcessBuilder directory sample code for java.lang.ProcessBuilder.directory(java.io.File) definition code for java.lang.ProcessBuilder.directory(java.io.File) (File sample code for java.io.File definition code for java.io.File  directory)
          Sets this process builder's working directory.
 Map sample code for java.util.Map definition code for java.util.Map <String sample code for java.lang.String definition code for java.lang.String ,String sample code for java.lang.String definition code for java.lang.String > environment sample code for java.lang.ProcessBuilder.environment() definition code for java.lang.ProcessBuilder.environment() ()
          Returns a string map view of this process builder's environment.
 boolean redirectErrorStream sample code for java.lang.ProcessBuilder.redirectErrorStream() definition code for java.lang.ProcessBuilder.redirectErrorStream() ()
          Tells whether this process builder merges standard error and standard output.
 ProcessBuilder sample code for java.lang.ProcessBuilder definition code for java.lang.ProcessBuilder redirectErrorStream sample code for java.lang.ProcessBuilder.redirectErrorStream(boolean) definition code for java.lang.ProcessBuilder.redirectErrorStream(boolean) (boolean redirectErrorStream)
          Sets this process builder's redirectErrorStream property.
 Process sample code for java.lang.Process definition code for java.lang.Process start sample code for java.lang.ProcessBuilder.start() definition code for java.lang.ProcessBuilder.start() ()
          Starts a new process using the attributes of this process builder.
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 

Constructor Detail

ProcessBuilder sample code for java.lang.ProcessBuilder(java.util.List<java.lang.String>) definition code for java.lang.ProcessBuilder(java.util.List<java.lang.String>)

public ProcessBuilder(List sample code for java.util.List definition code for java.util.List <String sample code for java.lang.String definition code for java.lang.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.

Parameters:
command - The list containing the program and its arguments
Throws:
NullPointerException sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - If the argument is null

ProcessBuilder sample code for java.lang.ProcessBuilder(java.lang.String...) definition code for java.lang.ProcessBuilder(java.lang.String...)

public ProcessBuilder(String sample code for java.lang.String definition code for java.lang.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.

Parameters:
command - A string array containing the program and its arguments
Method Detail

command sample code for java.lang.ProcessBuilder.command(java.util.List<java.lang.String>) definition code for java.lang.ProcessBuilder.command(java.util.List<java.lang.String>)

public ProcessBuilder sample code for java.lang.ProcessBuilder definition code for java.lang.ProcessBuilder  command(List sample code for java.util.List definition code for java.util.List <String sample code for java.lang.String definition code for java.lang.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.

Parameters:
command - The list containing the program and its arguments
Returns:
This process builder
Throws:
NullPointerException sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - If the argument is null

command sample code for java.lang.ProcessBuilder.command(java.lang.String...) definition code for java.lang.ProcessBuilder.command(java.lang.String...)

public ProcessBuilder sample code for java.lang.ProcessBuilder definition code for java.lang.ProcessBuilder  command(String sample code for java.lang.String definition code for java.lang.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.

Parameters:
command - A string array containing the program and its arguments
Returns:
This process builder

command sample code for java.lang.ProcessBuilder.command() definition code for java.lang.ProcessBuilder.command()

public List sample code for java.util.List definition code for java.util.List <String sample code for java.lang.String definition code for java.lang.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.

Returns:
This process builder's program and its arguments

environment sample code for java.lang.ProcessBuilder.environment() definition code for java.lang.ProcessBuilder.environment()

public Map sample code for java.util.Map definition code for java.util.Map <String sample code for java.lang.String definition code for java.lang.String ,String sample code for java.lang.String definition code for java.lang.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() sample code for java.lang.System.getenv() definition code for java.lang.System.getenv() ). Subprocesses subsequently started by this object's start() sample code for java.lang.ProcessBuilder.start() definition code for java.lang.ProcessBuilder.start() method will use this map as their environment.

The returned object may be modified using ordinary Map sample code for java.util.Map definition code for java.util.Map operations. These modifications will be visible to subprocesses started via the start() sample code for java.lang.ProcessBuilder.start() definition code for java.lang.ProcessBuilder.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 sample code for java.lang.System.getenv(java.lang.String) definition code for java.lang.System.getenv(java.lang.String) .

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 sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException . Attempting to query the presence of a key or value which is not of type String sample code for java.lang.String definition code for java.lang.String will throw a ClassCastException sample code for java.lang.ClassCastException definition code for java.lang.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 sample code for java.lang.UnsupportedOperationException definition code for java.lang.UnsupportedOperationException or IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.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) sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) and Object.hashCode() sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() methods.

The returned map is typically case-sensitive on all platforms.

If a security manager exists, its checkPermission sample code for java.lang.SecurityManager.checkPermission(java.security.Permission) definition code for java.lang.SecurityManager.checkPermission(java.security.Permission) method is called with a RuntimePermission sample code for java.lang.RuntimePermission definition code for java.lang.RuntimePermission ("getenv.*") permission. This may result in a SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException being thrown.

When passing information to a Java subprocess, system properties are generally preferred over environment variables.

Returns:
This process builder's environment
Throws:
SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - If a security manager exists and its checkPermission sample code for java.lang.SecurityManager.checkPermission(java.security.Permission) definition code for java.lang.SecurityManager.checkPermission(java.security.Permission) method doesn't allow access to the process environment
See Also:
Runtime.exec(String[],String[],java.io.File) sample code for java.lang.Runtime.exec(java.lang.String[], java.lang.String[], java.io.File) definition code for java.lang.Runtime.exec(java.lang.String[], java.lang.String[], java.io.File) , System.getenv() sample code for java.lang.System.getenv() definition code for java.lang.System.getenv()

directory sample code for java.lang.ProcessBuilder.directory() definition code for java.lang.ProcessBuilder.directory()

public File sample code for java.io.File definition code for java.io.File  directory()
Returns this process builder's working directory. Subprocesses subsequently started by this object's start() sample code for java.lang.ProcessBuilder.start() definition code for java.lang.ProcessBuilder.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.

Returns:
This process builder's working directory

directory sample code for java.lang.ProcessBuilder.directory(java.io.File) definition code for java.lang.ProcessBuilder.directory(java.io.File)

public ProcessBuilder sample code for java.lang.ProcessBuilder definition code for java.lang.ProcessBuilder  directory(File sample code for java.io.File definition code for java.io.File  directory)
Sets this process builder's working directory. Subprocesses subsequently started by this object's start() sample code for java.lang.ProcessBuilder.start() definition code for java.lang.ProcessBuilder.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.

Parameters:
directory - The new working directory
Returns:
This process builder

redirectErrorStream sample code for java.lang.ProcessBuilder.redirectErrorStream() definition code for java.lang.ProcessBuilder.redirectErrorStream()

public boolean redirectErrorStream()
Tells whether this process builder merges standard error and standard output.

If this property is true, then any error output generated by subprocesses subsequently started by this object's start() sample code for java.lang.ProcessBuilder.start() definition code for java.lang.ProcessBuilder.start() method will be merged with the standard output, so that both can be read using the Process.getInputStream() sample code for java.lang.Process.getInputStream() definition code for java.lang.Process.getInputStream() method. This makes it easier to correlate error messages with the corresponding output. The initial value is false.

Returns:
This process builder's redirectErrorStream property

redirectErrorStream sample code for java.lang.ProcessBuilder.redirectErrorStream(boolean) definition code for java.lang.ProcessBuilder.redirectErrorStream(boolean)

public ProcessBuilder sample code for java.lang.ProcessBuilder definition code for java.lang.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() sample code for java.lang.ProcessBuilder.start() definition code for java.lang.ProcessBuilder.start() method will be merged with the standard output, so that both can be read using the Process.getInputStream() sample code for java.lang.Process.getInputStream() definition code for java.lang.Process.getInputStream() method. This makes it easier to correlate error messages with the corresponding output. The initial value is false.

Parameters:
redirectErrorStream - The new property value
Returns:
This process builder

start sample code for java.lang.ProcessBuilder.start() definition code for java.lang.ProcessBuilder.start()

public Process sample code for java.lang.Process definition code for java.lang.Process  start()
              throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Starts a new process using the attributes of this process builder.

The new process will invoke the command and arguments given by command() sample code for java.lang.ProcessBuilder.command() definition code for java.lang.ProcessBuilder.command() , in a working directory as given by directory() sample code for java.lang.ProcessBuilder.directory() definition code for java.lang.ProcessBuilder.directory() , with a process environment as given by environment() sample code for java.lang.ProcessBuilder.environment() definition code for java.lang.ProcessBuilder.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.

If there is a security manager, its checkExec sample code for java.lang.SecurityManager.checkExec(java.lang.String) definition code for java.lang.SecurityManager.checkExec(java.lang.String) method is called with the first component of this object's command array as its argument. This may result in a SecurityException sample code for java.lang.SecurityException definition code for java.lang.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 sample code for java.io.IOException definition code for java.io.IOException .

Subsequent modifications to this process builder will not affect the returned Process sample code for java.lang.Process definition code for java.lang.Process .

Returns:
A new Process sample code for java.lang.Process definition code for java.lang.Process object for managing the subprocess
Throws:
NullPointerException sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - If an element of the command list is null
IndexOutOfBoundsException sample code for java.lang.IndexOutOfBoundsException definition code for java.lang.IndexOutOfBoundsException - If the command is an empty list (has size 0)
SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - If a security manager exists and its checkExec sample code for java.lang.SecurityManager.checkExec(java.lang.String) definition code for java.lang.SecurityManager.checkExec(java.lang.String) method doesn't allow creation of the subprocess
IOException sample code for java.io.IOException definition code for java.io.IOException - If an I/O error occurs
See Also:
Runtime.exec(String[], String[], java.io.File) sample code for java.lang.Runtime.exec(java.lang.String[], java.lang.String[], java.io.File) definition code for java.lang.Runtime.exec(java.lang.String[], java.lang.String[], java.io.File) , SecurityManager.checkExec(String) sample code for java.lang.SecurityManager.checkExec(java.lang.String) definition code for java.lang.SecurityManager.checkExec(java.lang.String)