Class SubProcessRunner


  • public class SubProcessRunner
    extends Object
    Helper class to run a subprocess and handle the process' input and output streams. This can be done to run a subprocess as an interactive shell, like this:
    
     SubProcessRunner runner = new SubProcessRunner();
     runner.setOut(System.out);
     runner.setErr(System.err);
     runner.exec(System.in, "sh", "-c" "cat file | less");
     
    It's main design is to handle simple commands with it's output. Setting the sub-process' input (part of exec(InputStream, String...) params) will act the same as controlling the programs input, whether it asks for key-presses or piped input.
    • Constructor Detail

      • SubProcessRunner

        public SubProcessRunner()
        Create a SubProcessRunner instance. By default it will ignore all output and wait forever for program to exit.
    • Method Detail

      • setRuntime

        public void setRuntime​(Runtime runtime)
        Set the runtime implementation used to run processes.
        Parameters:
        runtime - The runtime implementation.
      • setThreadFactory

        public void setThreadFactory​(ThreadFactory threadFactory)
        Parameters:
        threadFactory - Set the thread factory used to generate local threads used for handling IO piping.
      • setOut

        public void setOut​(OutputStream out)
        Set output stream to receive the programs standard output stream.
        Parameters:
        out - The output stream
      • setErr

        public void setErr​(OutputStream err)
        Set output stream to receive the programs standard error stream.
        Parameters:
        err - The output stream
      • setWorkingDir

        public void setWorkingDir​(Path workingDir)
        Set the working dir where the program should be run.
        Parameters:
        workingDir - The working dir path.
      • setDeadlineMs

        public void setDeadlineMs​(long deadlineMs)
        Set deadline in millis for the program to finish. If the program does not finish in the assigned deadline, it will be forcefully terminated and an IOException will be thrown from exec(String...).
        Parameters:
        deadlineMs - The deadline in MS.
      • setDeadlineFlushMs

        public void setDeadlineFlushMs​(long deadlineFlushMs)
        Set deadline in millis for the output handlers to finish after the program has exited. If the streams are not fully handled within that time, the exec(String...) method will throw an IOException. This is per default 1s, which will be the deadline of 0 is set.
        Parameters:
        deadlineFlushMs - The deadline in MS.
      • exec

        public int exec​(String... command)
                 throws IOException
        Execute command with no input. This will immediately close the stream to the process, so if it tries to read it will not block, but exit.
        Parameters:
        command - The command to be executed.
        Returns:
        The exit code of the program.
        Throws:
        IOException - If the program execution failed.
      • exec

        public int exec​(InputStream in,
                        String... command)
                 throws IOException
        Execute command with specified input.
        Parameters:
        in - The input stream to read input from.
        command - The command to be executed.
        Returns:
        The exit code of the program.
        Throws:
        IOException - If the program execution failed.