Class ProcessWrapper

java.lang.Object
com.pnfsoftware.jeb.util.concurrent.ProcessWrapper
All Implemented Interfaces:
AutoCloseable2, AutoCloseable

public class ProcessWrapper extends Object implements AutoCloseable2
A wrapper around Process to execute potentially long-running commands, that may require input. The process streams are accessible; it is the responsibility of the client to read (or drain) the output and error streams to avoid the chance of blocking on waitForCompletion(). This class is an auto-closable and may be treated as such (try-with-ARM).

A higher-level, safer alternative to consider if you do not need to handle the process streams is to use ProcessExec.

See Also:
  • Constructor Details

    • ProcessWrapper

      public ProcessWrapper(String... cmdarray)
      Execute a process without timeout. The caller is advised to take care of the target process output and error streams.
      Parameters:
      cmdarray - command
    • ProcessWrapper

      public ProcessWrapper(long timeoutMs, boolean redirectErrToOut, String... cmdarray)
      Execute a process with timeout.
      Parameters:
      timeoutMs - timeout in milliseconds before the process gets killed (<= 0 means no timeout)
      redirectErrToOut - true to redirect the process error stream to the output stream
      cmdarray - command
  • Method Details

    • start

      public ProcessWrapper start() throws IOException
      Start the process.
      Returns:
      this wrapper
      Throws:
      IOException - if the process cannot be started
    • kill

      public void kill()
      Terminate the process if it is still running and record its return code when available.
    • close

      public void close()
      Redirects to kill().
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface AutoCloseable2
    • waitForCompletion

      public void waitForCompletion()
      Wait for the process to complete, then update the return code. Calling this method without having reader threads draining the target process's output and error is dangerous unless a timeout was set: if the process generates a lot of output, it will stall indefinitely and block the current thread.
    • isAlive

      public boolean isAlive()
      Determine whether the wrapped process is running.
      Returns:
      true if the process was started and is still alive
    • getProcessInput

      public OutputStream getProcessInput()
      Get the process standard input stream.
      Returns:
      the process input stream, or null if the process has not been started
    • getProcessOutput

      public InputStream getProcessOutput()
      Get the process standard output stream.
      Returns:
      the process output stream, or null if the process has not been started
    • getProcessError

      public InputStream getProcessError()
      Get the process standard error stream.
      Returns:
      the process error stream, or null if the process has not been started
    • getReturnCode

      public Integer getReturnCode()
      Get the process return code.
      Returns:
      the process return code, or null if no return code has been recorded
    • toString

      public String toString()
      Overrides:
      toString in class Object