Class ProcessExecutor


  • public class ProcessExecutor
    extends Object
    Utility class that will execute sub processes via Runtime.getRuntime().exec(...) and read off the output from stderr and stdout of the sub process. This implementation uses a different thread to read each stream: the current thread for stdout and another, internal thread for stderr. This utility is able to handle concurrent executions, spawning as many threads as are required to handle the concurrent load.
    • Constructor Detail

      • ProcessExecutor

        public ProcessExecutor()
    • Method Detail

      • execute

        public static int execute​(String command)
        Executes the command via Runtime.getRuntime().exec() then writes stderr to log.error and stdout to log.info and blocks until the command is complete.
        Parameters:
        command - command string
        Returns:
        return code of command
        See Also:
        Runtime.exec(String)
      • execute

        public static int execute​(String[] commandParts)
        Executes the command via Runtime.getRuntime().exec() then writes stderr to log.error and stdout to log.info and blocks until the command is complete.
        Parameters:
        commandParts - command string
        Returns:
        return code of command
        See Also:
        Runtime.exec(String[])
      • execute

        public static int execute​(String[] commandParts,
                                  String outputStreamString)
        Executes the command via Runtime.getRuntime().exec(), writes outputStreamString to the process output stream if it is not null, then writes stderr to log.error and stdout to log.info and blocks until the command is complete.
        Parameters:
        commandParts - command string
        Returns:
        return code of command
        See Also:
        Runtime.exec(String[])
      • executeAndReturnResult

        public static String executeAndReturnResult​(String command)
      • executeAndReturnInterleavedOutput

        public static ProcessExecutor.ExitStatusAndOutput executeAndReturnInterleavedOutput​(String command)
        Execute the command and capture stdout and stderr.
        Returns:
        Exit status of command, and both stderr and stdout interleaved into stdout attribute.
      • executeAndReturnInterleavedOutput

        public static ProcessExecutor.ExitStatusAndOutput executeAndReturnInterleavedOutput​(String[] commandArray)
        Execute the command and capture stdout and stderr.
        Returns:
        Exit status of command, and both stderr and stdout interleaved into stdout attribute.