Class ProcessWrapper


  • public final class ProcessWrapper
    extends java.lang.Object
    ProcessWrapper encapsulates the steps needed to run a program using the ProcessBuilder class. To prevent side effects, ProcessWrapper decouples input commands from its internal ProcessBuilder and uses wrapper methods to isolate the state of the ProcessBuilder. ProcessWrapper removes direct access to all ProcessBuilder methods and allows only indirect access to ProcessBuilder.command() and ProcessBuilder.inheritIO().

    ProcessWrapper.getCommand() returns a copy of the internal ProcessBuilder command, and ProcessWrapper.setCommand() creates a copy of a new command for the internal ProcessBuilder. ProcessWrapper sets the redirectErrorStream property for the internal ProcessBuilder to true.

    Note that ProcessWrapper methods are not synchronized, and ProcessWrapper has not been tested with external synchronization.

    Following are some simple ProcessWrapper examples (using Python 2.7.16 on macOS Catalina):

          // Run a program and print the output:
          ProcessWrapper processWrapper = new ProcessWrapper("python", "-c", "print('Hello, world!')");
          processWrapper.run();
          processWrapper.printOutput();
          // Expected output:
          // Hello, world!
    
          // Modify the input:
          List<String> exampleCommand = processWrapper.getCommand();
          exampleCommand.remove(exampleCommand.size() - 1);
          exampleCommand.add("print('Hello')");
          processWrapper.setCommand(exampleCommand);
          processWrapper.run();
    
          // Work with the output:
          List<String> exampleOutput = processWrapper.getOutput();
          if (exampleOutput.size() > 0)
              System.out.println(exampleOutput.get(0) + " again!");
          // Expected output:
          // Hello again!
     
    • Constructor Detail

      • ProcessWrapper

        public ProcessWrapper​(String... command)
        Passes the command to the second constructor.
        Parameters:
        command - the command to be passed to the internal ProcessBuilder for execution
      • ProcessWrapper

        public ProcessWrapper​(List<String> command)
        Creates a ProcessWrapper and an internal ProcessBuilder using a copy of the command. The internal command cannot be modified outside of the constructed task builder (since the internal command List is a copy and Strings are immutable).
        Parameters:
        command - the command to be passed to the internal ProcessBuilder for execution
    • Method Detail

      • getCommand

        public List<String> getCommand()
        Returns a copy of the current ProcessBuilder command. Changes made to the returned command will not be reflected in the state of the internal ProcessBuilder (since the returned command List is a copy and Strings are immutable).
        Returns:
        a copy of the current ProcessBuilder command
      • setCommand

        public void setCommand​(List<String> command)
        Replaces the current command for the internal ProcessBuilder with a copy of the passed command. Changes made to the original command will not be reflected in the state of the internal ProcessBuilder (since the internal command List is a copy and Strings are immutable).
        Parameters:
        command - the new command to be copied into the internal ProcessBuilder
      • getOutput

        public List<String> getOutput()
        Returns a copy of the output from the command that the ProcessBuilder has executed.
        Returns:
        the output from the executed command
      • printOutput

        public void printOutput()
        Prints the output from the command that the ProcessBuilder has executed.