Class ProcessRunner


  • public class ProcessRunner
    extends Object
    • Constructor Detail

      • ProcessRunner

        public ProcessRunner​(String... command)
      • ProcessRunner

        public ProcessRunner​(List<String> command)
    • Method Detail

      • execWithCharset

        public static String execWithCharset​(String charset,
                                             String... command)
      • exec

        public ProcessRunner exec()

        Executes the given command as if it had been executed from the command line of the host OS (cmd.exe on windows, /bin/sh on *nix). The resulting exit code and the output buffer are accessible after this call, with getExitCode() and getBuffer(), respectively.

        This method blocks on the execution of the command.

        Example Usages:

           var currentDir = new ProcessRunner("dir").exec() // windows
           var currentDir = new ProcessRunner("ls").exec() // *nix
           new ProcessRunner( "rm -rf " + directoryToNuke ).exec()
         

        Returns:
        this object for chaining
      • getRawCmdStr

        public String getRawCmdStr()
      • getBuffer

        public String getBuffer()
        Returns any output buffered from the process' stdout or stderr, depending on if withStdOutBuffered() and/or withStdErrBuffered() were used. If a buffer was desired and the process printed nothing out, an empty string is returned.
        Returns:
        the buffer, or null if nothing was to be buffered
      • getExitCode

        public Integer getExitCode()
        Returns the process' exit code, if it finished.
        Returns:
        the exit code, or null if the process never completed
      • withWorkingDirectory

        public ProcessRunner withWorkingDirectory​(File dir)
        Sets this process' working directory.
        Parameters:
        dir - this process' working directory
        Returns:
        this object for chaining
      • withArg

        public ProcessRunner withArg​(String arg)
        Adds an argument to the command.
        Parameters:
        arg - the command line argument to add
        Returns:
        this object for chaining
      • withEnvironmentVariable

        public ProcessRunner withEnvironmentVariable​(String name,
                                                     String value)
        Adds a name-value pair into this process' environment. This can be called multiple times in a chain to set multiple environment variables.
        Parameters:
        name - the variable name
        value - the variable value
        Returns:
        this object for chaining
        See Also:
        ProcessBuilder, System.getenv()
      • withCharset

        public ProcessRunner withCharset​(String cs)
        Sets the charset with which to write to this process' input and read its output. If unused, this process will by default use UTF-8.
        Parameters:
        cs - the charset to use
        Returns:
        this object for chaining
      • withStdOutBuffered

        public ProcessRunner withStdOutBuffered()
        Sets this process' stdout stream to be stored in the buffer accessible by getBuffer().
        Returns:
        this object for chaining
      • withStdErrBuffered

        public ProcessRunner withStdErrBuffered()
        Sets this process' stdout stream to be stored in the buffer accessible by getBuffer().
        Returns:
        this object for chaining
      • withEcho

        public ProcessRunner withEcho()
        Sets this process' output to be displayed the parent process' stdout and stderr.
        Returns:
        this object for chaining
      • input

        public ProcessRunner input​(String input)
        Sets the text to be directed into this process' stdin.
        Parameters:
        input - the text to direct into stdin
        Returns:
        this object for chaining
      • withCMD

        public ProcessRunner withCMD()
        The process built up will used CMD.EXE if this is a windows platform. This is necessary because on windows certain basic commands such as "dir" are not programs, but rather are built into CMD. Thanks, Microsoft.
        Returns:
        this object for chaining
      • withStdErrHandler

        public ProcessRunner withStdErrHandler​(OutputHandler stdErrHandler)
        Adds a block to handle lines output this process' stderr. This can be called multiple times in a chain to add multiple handlers.
        Parameters:
        stdErrHandler - handler that will be called with every line of output to stderr
        Returns:
        this object for chaining
      • withStdOutHandler

        public ProcessRunner withStdOutHandler​(OutputHandler stdOutHandler)
        Adds a block to handle lines output this process' stdout. This can be called multiple times in a chain to add multiple handlers.
        Parameters:
        stdOutHandler - handler that will be called with every line of output to stdout
        Returns:
        this object for chaining