Package com.day.util

Class ProcessRunner

  • All Implemented Interfaces:
    java.lang.Runnable

    public class ProcessRunner
    extends java.lang.Object
    implements java.lang.Runnable
    The ProcessRunner class helps running external processes. This encompasses redirection of stdin, stdout and stderr as well as optionally waiting for the completion of the process. This implementation is based on the Runtime.exec(String) method and does not yet support passing specific environments or decomposed comand lines.

    This class can be used in two ways. If you don't care about waiting for the processes completion and return code, you might do it like this :

    Runnable pr = new ProcessRunner("command", null, null, null);
    new Thread(pr).start();

    If on the other hand you want to capture all output and also keep an eye on the time the process takes to execute (or wait indefinitely), you might use the class like this :

    ProcessRunner pr = new ProcessRunner("command", in, out, err);
    pr.run(0);
    int rc = pr.getReturnCode();
    Since:
    coati Audience wad
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int PROCESS_ABORTED
      Constant to indicate the process has been aborted
      static int PROCESS_RUNNING
      Constant to indicate the process is running
    • Constructor Summary

      Constructors 
      Constructor Description
      ProcessRunner​(java.lang.String cmdLine)
      Creates a new ProcessRunner to execute the given command line containing the command to execute and all relevant command arguments with no redirection of stdin, stdout and stderr.
      ProcessRunner​(java.lang.String cmdLine, java.io.InputStream stdin, java.io.OutputStream stdout, java.io.OutputStream stderr)
      Creates a new ProcessRunner to execute the given command line containing the command to execute and all relevant command arguments.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int getReturnCode()
      Returns the return code from running the command line.
      void run()
      Runs the process sending the input data and capturing output data.
      void run​(long waitTime)
      Executes the command line and waits for the completion of the prcoess.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • PROCESS_RUNNING

        public static final int PROCESS_RUNNING
        Constant to indicate the process is running
        See Also:
        Constant Field Values
      • PROCESS_ABORTED

        public static final int PROCESS_ABORTED
        Constant to indicate the process has been aborted
        See Also:
        Constant Field Values
    • Constructor Detail

      • ProcessRunner

        public ProcessRunner​(java.lang.String cmdLine,
                             java.io.InputStream stdin,
                             java.io.OutputStream stdout,
                             java.io.OutputStream stderr)
        Creates a new ProcessRunner to execute the given command line containing the command to execute and all relevant command arguments. The stdin InputStream can be used to feed input data to the command executed, while the output (stdout and stderr) are redirected to the given OutputStreams or the process defaults (System.out and System.err, resp.).

        None of the streams is closed after running the command line. This is the sole duty of the client of this class.

        Parameters:
        cmdLine - This commandline is given to the Runtime.exec(String) contains all the command arguments and is split up with a StringTokenizer. See the API docs on the Runtime.exec(String) method for details.
        stdin - The InputStream containing data to be handed to the process as stdin. Set this to null if the process should not get any input. This stream is completely read after the process is started and sent to the process. Therefor the stream should be available.
        stdout - The OutputStream to send the stdout output of the process to. If this is null, stdout output is written to System.out.
        stderr - The OuputStream to send the stderr output of the process to. If this is null, stderr output is written to System.err.
      • ProcessRunner

        public ProcessRunner​(java.lang.String cmdLine)
        Creates a new ProcessRunner to execute the given command line containing the command to execute and all relevant command arguments with no redirection of stdin, stdout and stderr.
        Parameters:
        cmdLine - This commandline is given to the Runtime.exec(String) contains all the command arguments and is split up with a StringTokenizer. See the API docs on the Runtime.exec(String) method for details.
    • Method Detail

      • run

        public void run​(long waitTime)
        Executes the command line and waits for the completion of the prcoess. The process runs in its own thread which is marked as daemon thread. That is as soon as the Java VM is about to end, the process will also forcibly stopped and does not run to completion.
        Parameters:
        waitTime - The number of milliseconds to wait for the completion of the process. If the time has ellapsed before the process has terminated, the process will be forcibly terminated. If this value is negative, the process runs completely detached, while a value of zero indicates to wait indeterminate for the completion of the process.
      • getReturnCode

        public int getReturnCode()
        Returns the return code from running the command line. As long as the command is running, the method returns PROCESS_RUNNING. After the process has terminated, the method returns the return code from the process or PROCESS_ABORTED if the command has been terminated due to a timeout.
        Returns:
        The return code from the process or either PROCESS_RUNNING or PROCESS_ABORTED.
      • run

        public void run()
        Runs the process sending the input data and capturing output data.

        Do not directly call this method. Instead either use new Thread(new ProcessRunner(cmd)).start() or use the run(long) method.

        Specified by:
        run in interface java.lang.Runnable