Module java.base
Package java.lang

Class ProcessBuilder

java.lang.Object
java.lang.ProcessBuilder

public final classProcessBuilderextendsObject
This class is used to create operating system processes.

EachProcessBuilder instance manages a collection of process attributes. Thestart() method creates a newProcess instance with those attributes. Thestart() method can be invoked repeatedly from the same instance to create new subprocesses with identical or related attributes.

ThestartPipeline 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:

  • acommand, a list of strings which signifies the external program file to be invoked and its arguments, if any. Which string lists represent a valid operating system command is system-dependent. For example, it is common for each conceptual argument to be an element in this list, but there are operating systems where programs are expected to tokenize command line strings themselves - on such a system a Java implementation might require commands to contain exactly two elements.
  • anenvironment, which is a system-dependent mapping fromvariables tovalues. The initial value is a copy of the environment of the current process (seeSystem.getenv()).
  • aworking directory. The default value is the current working directory of the current process, usually the directory named by the system propertyuser.dir.
  • a source ofstandard input. By default, the subprocess reads input from a pipe. Java code can access this pipe via the output stream returned byProcess.getOutputStream(). However, standard input may be redirected to another source usingredirectInput. In this case,Process.getOutputStream() will return anull output stream, for which:
    • thewrite methods always throwIOException
    • theclose method does nothing
  • a destination forstandard output andstandard error. By default, the subprocess writes standard output and standard error to pipes. Java code can access these pipes via the input streams returned byProcess.getInputStream() andProcess.getErrorStream(). However, standard output and standard error may be redirected to other destinations usingredirectOutput andredirectError. In this case,Process.getInputStream() and/orProcess.getErrorStream() will return anull input stream, for which:
    • theread methods always return-1
    • theavailable method always returns0
    • theclose method does nothing
  • aredirectErrorStream property. Initially, this property isfalse, meaning that the standard output and error output of a subprocess are sent to two separate streams, which can be accessed using theProcess.getInputStream() andProcess.getErrorStream() methods.

    If the value is set totrue, then:

    • standard error is merged with the standard output and always sent to the same destination (this makes it easier to correlate error messages with the corresponding output)
    • the common destination of standard error and standard output can be redirected usingredirectOutput
    • any redirection set by theredirectError method is ignored when creating a subprocess
    • the stream returned fromProcess.getErrorStream() will always be anull input stream

Modifying a process builder's attributes will affect processes subsequently started by that object'sstart() method, but will never affect previously started processes or the Java process itself.

Most error checking is performed by thestart() method. It is possible to modify the state of an object so thatstart() will fail. For example, setting the command attribute to an empty list will not throw an exception unlessstart() is invoked.

Note that this class is not synchronized. If multiple threads access aProcessBuilder instance concurrently, and at least one of the threads modifies one of the attributes structurally, itmust 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 callMap.clear() before adding environment variables.

Unless otherwise noted, passing anull argument to a constructor or method in this class will cause aNullPointerException to be thrown.

Since:
1.5