APIdia Launch Partner
Top Description Inners Fields Constructors Methods
java.lang

public final Class ProcessBuilder

extends Object
Class Inheritance
Imports
jdk.internal.util.OperatingSystem, java.io.File, .FileDescriptor, .IOException, .InputStream, .OutputStream, java.util.Arrays, .ArrayList, .List, .Map, jdk.internal.event.ProcessStartEvent

This class is used to create operating system processes.

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:

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();
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;
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.

Author
Martin Buchholz
Since
1.5

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
pack-priv static class
public abstract static class
ProcessBuilder.Redirect

Represents a source of subprocess input or a destination of subprocess output.

pack-priv static class
ProcessBuilder.RedirectPipeImpl

Private implementation subclass of Redirect that holds a FileDescriptor for the output of a previously started Process.

Field Summary

Modifier and TypeField and Description
private List<String>
private File
private Map<String, String>
private static System.Logger
private boolean
private ProcessBuilder.Redirect[]

Constructor Summary

AccessConstructor and Description
public
ProcessBuilder(List<String>
the list containing the program and its arguments
command
)

Constructs a process builder with the specified operating system program and arguments.

public
ProcessBuilder(String...
a string array containing the program and its arguments
command
)

Constructs a process builder with the specified operating system program and arguments.

Method Summary

Modifier and TypeMethod and Description
public ProcessBuilder

Returns:

this process builder
command
(List<String>
the list containing the program and its arguments
command
)

Sets this process builder's operating system program and arguments.

public ProcessBuilder

Returns:

this process builder
command
(String...
a string array containing the program and its arguments
command
)

Sets this process builder's operating system program and arguments.

public List<String>

Returns:

this process builder's program and its arguments
command
()

Returns this process builder's operating system program and arguments.

public File

Returns:

this process builder's working directory
directory
()

Returns this process builder's working directory.

public ProcessBuilder

Returns:

this process builder
directory
(File
the new working directory
directory
)

Sets this process builder's working directory.

public Map<String, String>

Returns:

this process builder's environment
environment
()

Returns a string map view of this process builder's environment.

pack-priv ProcessBuilder
public ProcessBuilder

Returns:

this process builder
inheritIO
()

Sets the source and destination for subprocess standard I/O to be the same as those of the current Java process.

public ProcessBuilder

Returns:

this process builder
redirectError
(ProcessBuilder.Redirect
the new standard error destination
destination
)

Sets this process builder's standard error destination.

public ProcessBuilder

Returns:

this process builder
redirectError
(File
the new standard error destination
file
)

Sets this process builder's standard error destination to a file.

public ProcessBuilder.Redirect

Returns:

this process builder's standard error destination
redirectError
()

Returns this process builder's standard error destination.

public boolean

Returns:

this process builder's redirectErrorStream property
redirectErrorStream
()

Tells whether this process builder merges standard error and standard output.

public ProcessBuilder

Returns:

this process builder
redirectErrorStream
(boolean
the new property value
redirectErrorStream
)

Sets this process builder's redirectErrorStream property.

public ProcessBuilder

Returns:

this process builder
redirectInput
(ProcessBuilder.Redirect
the new standard input source
source
)

Sets this process builder's standard input source.

public ProcessBuilder

Returns:

this process builder
redirectInput
(File
the new standard input source
file
)

Sets this process builder's standard input source to a file.

public ProcessBuilder.Redirect

Returns:

this process builder's standard input source
redirectInput
()

Returns this process builder's standard input source.

public ProcessBuilder

Returns:

this process builder
redirectOutput
(ProcessBuilder.Redirect
the new standard output destination
destination
)

Sets this process builder's standard output destination.

public ProcessBuilder

Returns:

this process builder
redirectOutput
(File
the new standard output destination
file
)

Sets this process builder's standard output destination to a file.

public ProcessBuilder.Redirect

Returns:

this process builder's standard output destination
redirectOutput
()

Returns this process builder's standard output destination.

private ProcessBuilder.Redirect[]

Returns:

the array of redirects
redirects
()

Return the array of redirects, creating the default as needed.

public Process

Returns:

a new Process object for managing the subprocess
start
()

Starts a new process using the attributes of this process builder.

private Process

Returns:

the new Process
start
(ProcessBuilder.Redirect[]
array of redirects for stdin, stdout, stderr
redirects
)

Start a new Process using an explicit array of redirects.

public static List<Process>

Returns:

a List<Process>es started from the corresponding ProcessBuilder
startPipeline
(List<ProcessBuilder>
a List of ProcessBuilders
builders
)

Starts a Process for each ProcessBuilder, creating a pipeline of processes linked by their standard output and standard input streams.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait