Package java.lang
Class ProcessBuilder
java.lang.Object
java.lang.ProcessBuilder
public final class ProcessBuilder extends Object
Creates operating system processes. See
Process for documentation and
example usage.-
Constructor Summary
Constructors Constructor Description ProcessBuilder(String... command)Constructs a newProcessBuilderinstance with the specified operating system program and its arguments.ProcessBuilder(List<String> command)Constructs a newProcessBuilderinstance with the specified operating system program and its arguments. -
Method Summary
Modifier and Type Method Description List<String>command()Returns this process builder's current program and arguments.ProcessBuildercommand(String... command)Changes the program and arguments of this process builder.ProcessBuildercommand(List<String> command)Changes the program and arguments of this process builder.Filedirectory()Returns the working directory of this process builder.ProcessBuilderdirectory(File directory)Changes the working directory of this process builder.Map<String,String>environment()Returns this process builder's current environment.booleanredirectErrorStream()Indicates whether the standard error should be redirected to standard output.ProcessBuilderredirectErrorStream(boolean redirectErrorStream)Changes the state of whether or not standard error is redirected to standard output.Processstart()Starts a new process based on the current state of this process builder.
-
Constructor Details
-
ProcessBuilder
Constructs a newProcessBuilderinstance with the specified operating system program and its arguments.- Parameters:
command- the requested operating system program and its arguments.
-
ProcessBuilder
Constructs a newProcessBuilderinstance with the specified operating system program and its arguments. Note that the list passed to this constructor is not copied, so any subsequent updates to it are reflected in this instance's state.- Parameters:
command- the requested operating system program and its arguments.- Throws:
NullPointerException- ifcommandisnull.
-
-
Method Details
-
command
Returns this process builder's current program and arguments. Note that the returned list is not a copy and modifications to it will change the state of this instance.- Returns:
- this process builder's program and arguments.
-
command
Changes the program and arguments of this process builder.- Parameters:
command- the new operating system program and its arguments.- Returns:
- this process builder instance.
-
command
Changes the program and arguments of this process builder. Note that the list passed to this method is not copied, so any subsequent updates to it are reflected in this instance's state.- Parameters:
command- the new operating system program and its arguments.- Returns:
- this process builder instance.
- Throws:
NullPointerException- ifcommandisnull.
-
directory
Returns the working directory of this process builder. Ifnullis returned, then the working directory of the Java process is used when a process is started.- Returns:
- the current working directory, may be
null.
-
directory
Changes the working directory of this process builder. If the specified directory isnull, then the working directory of the Java process is used when a process is started.- Parameters:
directory- the new working directory for this process builder.- Returns:
- this process builder instance.
-
environment
Returns this process builder's current environment. When a process builder instance is created, the environment is populated with a copy of the environment, as returned bySystem.getenv(). Note that the map returned by this method is not a copy and any changes made to it are reflected in this instance's state.- Returns:
- the map containing this process builder's environment variables.
-
redirectErrorStream
public boolean redirectErrorStream()Indicates whether the standard error should be redirected to standard output. If redirected, theProcess.getErrorStream()will always return end of stream and standard error is written toProcess.getInputStream().- Returns:
trueif the standard error is redirected;falseotherwise.
-
redirectErrorStream
Changes the state of whether or not standard error is redirected to standard output.- Parameters:
redirectErrorStream-trueto redirect standard error,falseotherwise.- Returns:
- this process builder instance.
-
start
Starts a new process based on the current state of this process builder.- Returns:
- the new
Processinstance. - Throws:
NullPointerException- if any of the elements ofcommand()isnull.IndexOutOfBoundsException- ifcommand()is empty.IOException- if an I/O error happens.
-