Class ProcessExecutor<E extends Exception>

  • Type Parameters:
    E - The type of the exceptions the handle* methods may throw.

    public class ProcessExecutor<E extends Exception>
    extends Object
    This class can be used to execute a separate process and read its output in a convenient way. It is only useful for processes which handle only one task and exit afterwards.

    This class is not thread-safe, it assumes that never two of its methods are executed simultaneously.

    When an instance of this class is created, the corresponding process is started immediately. Then some text may be written to stdin of the process with the println(String) method. Afterwards join() has to be called, which reads the output from the process and calls the handle* methods. This method blocks, i.e. when it returns the process has terminated. Now the get* methods may be used to get the output of the process.

    • Method Detail

      • println

        public void println​(String s)
                     throws IOException
        Write a String to the process. May only be called before join() was called, as afterwards the process is not running anymore.
        Throws:
        IOException
      • print

        public void print​(String s)
                   throws IOException
        Write a String to the process. May only be called before join() was called, as afterwards the process is not running anymore.
        Throws:
        IOException
      • sendEOF

        public void sendEOF()
                     throws IOException
        Sends the EOF (end of file) signal to stdin of the process.
        Throws:
        IOException
      • handleOutput

        protected void handleOutput​(String line)
                             throws E extends Exception
        Handle one line of output from the process. This method may be overwritten by clients. The default implementation logs the line on level ALL and adds it to a list which may later be retrieved with getOutput(). It never throws an exception (but client implementations may do so).

        This method will be called in a new thread.

        Throws:
        E - Overwriting methods may throw this exception which will be propagated.
        E extends Exception
      • handleErrorOutput

        protected void handleErrorOutput​(String line)
                                  throws E extends Exception
        Handle one line of stderr output from the process. This method may be overwritten by clients. The default implementation logs the line on level WARNING and adds it to a list which may later be retrieved with getErrorOutput(). It never throws an exception (but client implementations may do so).

        This method will be called in a new thread.

        Throws:
        E - Overwriting methods may throw this exception which will be propagated.
        E extends Exception
      • handleExitCode

        protected void handleExitCode​(int code)
                               throws E extends Exception
        Handle the exit code of the process. This method may be overwritten by clients. The default implementation logs the code on level WARNING, if it is non-zero.

        This method will be called in a new thread.

        Throws:
        E - Overwriting methods may throw this exception which will be propagated.
        E extends Exception
      • isFinished

        public boolean isFinished()
        Checks whether the process has finished already. This is true exactly if join() has been called.
      • getOutput

        public List<String> getOutput()
        Returns the complete output of the process. May only be called after join() has been called.
      • getErrorOutput

        public List<String> getErrorOutput()
        Returns the complete output to stderr of the process. May only be called after join() has been called.