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 Details

    • ProcessBuilder

      public ProcessBuilder​(String... command)
      Constructs a new ProcessBuilder instance with the specified operating system program and its arguments.
      Parameters:
      command - the requested operating system program and its arguments.
    • ProcessBuilder

      public ProcessBuilder​(List<String> command)
      Constructs a new ProcessBuilder instance 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 - if command is null.
  • Method Details

    • command

      public List<String> 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

      public ProcessBuilder command​(String... 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

      public ProcessBuilder command​(List<String> 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 - if command is null.
    • directory

      public File directory()
      Returns the working directory of this process builder. If null is 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

      public ProcessBuilder directory​(File directory)
      Changes the working directory of this process builder. If the specified directory is null, 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

      public Map<String,​String> 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 by System.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, the Process.getErrorStream() will always return end of stream and standard error is written to Process.getInputStream().
      Returns:
      true if the standard error is redirected; false otherwise.
    • redirectErrorStream

      public ProcessBuilder redirectErrorStream​(boolean redirectErrorStream)
      Changes the state of whether or not standard error is redirected to standard output.
      Parameters:
      redirectErrorStream - true to redirect standard error, false otherwise.
      Returns:
      this process builder instance.
    • start

      public Process start() throws IOException
      Starts a new process based on the current state of this process builder.
      Returns:
      the new Process instance.
      Throws:
      NullPointerException - if any of the elements of command() is null.
      IndexOutOfBoundsException - if command() is empty.
      IOException - if an I/O error happens.