Class ProcessUtil


  • public final class ProcessUtil
    extends Object
    Utility class for invoking system processes.
    • Method Detail

      • invokeProcess

        public static int invokeProcess​(String[] commandLine)
                                 throws IOException,
                                        InterruptedException
        Runs the given set of command line arguments as a system process and returns the exit value of the spawned process. Discards any output of the process.
        Parameters:
        commandLine - the list of command line arguments to run
        Returns:
        the exit code of the process
        Throws:
        IOException - if an exception occurred while reading the process' outputs
        InterruptedException - if an exception occurred during process exception
      • invokeProcess

        public static int invokeProcess​(List<String> commandLine)
                                 throws IOException,
                                        InterruptedException
        Runs the given set of command line arguments as a system process and returns the exit value of the spawned process. Discards any output of the process.
        Parameters:
        commandLine - the list of command line arguments to run
        Returns:
        the exit code of the process
        Throws:
        IOException - if an exception occurred while reading the process' outputs
        InterruptedException - if an exception occurred during process exception
      • invokeProcess

        public static int invokeProcess​(String[] commandLine,
                                        Reader input)
                                 throws IOException,
                                        InterruptedException
        Runs the given set of command line arguments as a system process and returns the exit value of the spawned process. Additionally, allows to supply an input stream to the invoked program. Discards any output of the process.
        Parameters:
        commandLine - the list of command line arguments to run
        input - the input passed to the program
        Returns:
        the exit code of the process
        Throws:
        IOException - if an exception occurred while reading the process' outputs, or writing the process' inputs
        InterruptedException - if an exception occurred during process exception
      • invokeProcess

        public static int invokeProcess​(List<String> commandLine,
                                        Reader input)
                                 throws IOException,
                                        InterruptedException
        Runs the given set of command line arguments as a system process and returns the exit value of the spawned process. Additionally, allows to supply an input stream to the invoked program. Discards any output of the process.
        Parameters:
        commandLine - the list of command line arguments to run
        input - the input passed to the program
        Returns:
        the exit code of the process
        Throws:
        IOException - if an exception occurred while reading the process' outputs, or writing the process' inputs
        InterruptedException - if an exception occurred during process exception
      • invokeProcess

        public static int invokeProcess​(String[] commandLine,
                                        Consumer<String> consumer)
                                 throws IOException,
                                        InterruptedException
        Runs the given set of command line arguments as a system process and returns the exit value of the spawned process. Outputs of the process (both normal and error) are passed to the consumer.
        Parameters:
        commandLine - the list of command line arguments to run
        consumer - the consumer for the program's output
        Returns:
        the exit code of the process
        Throws:
        IOException - if an exception occurred while reading the process' outputs
        InterruptedException - if an exception occurred during process exception
      • invokeProcess

        public static int invokeProcess​(List<String> commandLine,
                                        Consumer<String> consumer)
                                 throws IOException,
                                        InterruptedException
        Runs the given set of command line arguments as a system process and returns the exit value of the spawned process. Outputs of the process (both normal and error) are passed to the consumer.
        Parameters:
        commandLine - the list of command line arguments to run
        consumer - the consumer for the program's output
        Returns:
        the exit code of the process
        Throws:
        IOException - if an exception occurred while reading the process' outputs
        InterruptedException - if an exception occurred during process exception
      • invokeProcess

        public static int invokeProcess​(String[] commandLine,
                                        Reader input,
                                        Consumer<String> consumer)
                                 throws IOException,
                                        InterruptedException
        Runs the given set of command line arguments as a system process and returns the exit value of the spawned process. Additionally, allows to supply an input stream to the invoked program. Outputs of the process (both normal and error) are passed to the consumer.
        Parameters:
        commandLine - the list of command line arguments to run
        input - the input passed to the program
        consumer - the consumer for the program's output
        Returns:
        the exit code of the process
        Throws:
        IOException - if an exception occurred while reading the process' outputs, or writing the process' inputs
        InterruptedException - if an exception occurred during process exception
      • invokeProcess

        public static int invokeProcess​(List<String> commandLine,
                                        Reader input,
                                        Consumer<String> consumer)
                                 throws IOException,
                                        InterruptedException
        Runs the given set of command line arguments as a system process and returns the exit value of the spawned process. Additionally, allows to supply an input stream to the invoked program. Outputs of the process (both normal and error) are passed to the consumer.
        Parameters:
        commandLine - the list of command line arguments to run
        input - the input passed to the program
        consumer - the consumer for the program's output
        Returns:
        the exit code of the process
        Throws:
        IOException - if an exception occurred while reading the process' outputs, or writing the process' inputs
        InterruptedException - if an exception occurred during process exception
      • buildProcess

        public static Process buildProcess​(String[] commandLine,
                                           @Nullable Reader input,
                                           @Nullable Consumer<String> stdOutConsumer,
                                           @Nullable Consumer<String> stdErrConsumer)
                                    throws IOException
        Builds and starts a system process for the given set of command line arguments. Additionally, allows to supply an input stream to the invoked program, as well as independent consumers for the process' standard and error output.

        The consumers for the process' outputs run in separate threads, preventing potential deadlock scenarios where client code waits for the process' termination (e.g. Process.waitFor()) which is blocked by full system buffers.

        Parameters:
        commandLine - the list of command line arguments to run
        input - the input passed to the program, maybe be null if the process expects no input
        stdOutConsumer - the consumer for the programs output
        stdErrConsumer - the consumer for the programs output
        Returns:
        the reference to the running process
        Throws:
        IOException - if an exception occurred while reading the process outputs