Class ProcessRunner


  • public final class ProcessRunner
    extends java.lang.Object
    Utility class providing platform command execution support.
    • Constructor Summary

      Constructors 
      Constructor Description
      ProcessRunner​(java.lang.String... command)
      Constructs a new ProcessRunner instance.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void checkStatus()
      Checks the status of the last run() invocation and throws an I/O exception in case an error occurred or if the process' exit status is not 0.
      int exitValue()
      Gets the process exit value of the last run() invocation.
      java.lang.String output()
      Gets the process output captured during the last run() invocation.
      ProcessRunner.Status run()
      Runs the command and waits until either the process completes or an error occurs.
      java.util.Optional<java.io.IOException> statusException()
      Gets the optional IOException caught during the last run() invocation.
      java.lang.String toString()  
      ProcessRunner withCaptureOutput​(boolean capture)
      Sets the capture-output option for process execution.
      ProcessRunner withTimeout​(long timeout)
      Sets the timeout (in milliseconds) to wait for process completion.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • ProcessRunner

        public ProcessRunner​(java.lang.String... command)
        Constructs a new ProcessRunner instance.
        Parameters:
        command - the command line to run.
    • Method Detail

      • withTimeout

        public ProcessRunner withTimeout​(long timeout)
        Sets the timeout (in milliseconds) to wait for process completion.
        Parameters:
        timeout - the timeout to use.
        Returns:
        the updated ProcessRunner instance.
      • withCaptureOutput

        public ProcessRunner withCaptureOutput​(boolean capture)
        Sets the capture-output option for process execution.
        Parameters:
        capture - whether to capture the running's process output (true) or not (false).
        Returns:
        the updated ProcessRunner instance.
      • run

        public ProcessRunner.Status run()
        Runs the command and waits until either the process completes or an error occurs.
        Returns:
        the new status of this ProcessRunner instance.
      • checkStatus

        public void checkStatus()
                         throws java.io.IOException
        Checks the status of the last run() invocation and throws an I/O exception in case an error occurred or if the process' exit status is not 0.
        Throws:
        java.io.IOException - if an error occurred during
      • statusException

        public java.util.Optional<java.io.IOException> statusException()
        Gets the optional IOException caught during the last run() invocation.
        Returns:
        the optional IOException caught during the last run() invocation.
      • exitValue

        public int exitValue()
        Gets the process exit value of the last run() invocation.
        Returns:
        the process exit value of the last run() invocation.
      • output

        public java.lang.String output()
        Gets the process output captured during the last run() invocation.
        Returns:
        the process output captured during the last run() invocation.
        See Also:
        withCaptureOutput(boolean)
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object