Package gw.util.process
Class ProcessRunner
- java.lang.Object
-
- gw.util.process.ProcessRunner
-
public class ProcessRunner extends java.lang.Object
-
-
Field Summary
Fields Modifier and Type Field Description private OutputBuffer_bufferprivate boolean_bufferStdErrprivate boolean_bufferStdOutprivate java.lang.String_charsetprivate boolean_echoprivate java.util.Map<java.lang.String,java.lang.String>_envprivate java.lang.Integer_exitCodeprivate java.lang.String_inputprivate java.util.List<java.lang.String>_rawCmdprivate java.util.List<OutputHandler>_stdErrHandlersprivate java.util.List<OutputHandler>_stdOutHandlersprivate boolean_withCMDprivate java.io.File_workingDirectory
-
Constructor Summary
Constructors Constructor Description ProcessRunner(java.lang.String... command)ProcessRunner(java.util.List<java.lang.String> command)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ProcessRunnerexec()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).static java.lang.Stringexec(java.lang.String... command)static java.lang.StringexecWithCharset(java.lang.String charset, java.lang.String... command)private static voidfeedInput(java.lang.Process process, java.lang.String input, java.lang.String charset)java.lang.StringgetBuffer()Returns any output buffered from the process' stdout or stderr, depending on ifwithStdOutBuffered()and/orwithStdErrBuffered()were used.java.lang.IntegergetExitCode()Returns the process' exit code, if it finished.java.lang.StringgetRawCmdStr()ProcessRunnerinput(java.lang.String input)Sets the text to be directed into this process' stdin.private static java.lang.IntegernomNomNom(java.lang.Process process, OutputHandler stdOut, OutputHandler stdErr, java.lang.String charset)ProcessRunnerwithArg(java.lang.String arg)Adds an argument to the command.ProcessRunnerwithCharset(java.lang.String cs)Sets the charset with which to write to this process' input and read its output.ProcessRunnerwithCMD()The process built up will used CMD.EXE if this is a windows platform.ProcessRunnerwithEcho()Sets this process' output to be displayed the parent process' stdout and stderr.ProcessRunnerwithEnvironmentVariable(java.lang.String name, java.lang.String value)Adds a name-value pair into this process' environment.ProcessRunnerwithStdErrBuffered()Sets this process' stdout stream to be stored in the buffer accessible bygetBuffer().ProcessRunnerwithStdErrHandler(OutputHandler stdErrHandler)Adds a block to handle lines output this process' stderr.ProcessRunnerwithStdOutBuffered()Sets this process' stdout stream to be stored in the buffer accessible bygetBuffer().ProcessRunnerwithStdOutHandler(OutputHandler stdOutHandler)Adds a block to handle lines output this process' stdout.ProcessRunnerwithWorkingDirectory(java.io.File dir)Sets this process' working directory.
-
-
-
Field Detail
-
_rawCmd
private final java.util.List<java.lang.String> _rawCmd
-
_buffer
private OutputBuffer _buffer
-
_exitCode
private java.lang.Integer _exitCode
-
_withCMD
private boolean _withCMD
-
_workingDirectory
private java.io.File _workingDirectory
-
_env
private java.util.Map<java.lang.String,java.lang.String> _env
-
_echo
private boolean _echo
-
_input
private java.lang.String _input
-
_bufferStdOut
private boolean _bufferStdOut
-
_bufferStdErr
private boolean _bufferStdErr
-
_stdOutHandlers
private java.util.List<OutputHandler> _stdOutHandlers
-
_stdErrHandlers
private java.util.List<OutputHandler> _stdErrHandlers
-
_charset
private java.lang.String _charset
-
-
Method Detail
-
execWithCharset
public static java.lang.String execWithCharset(java.lang.String charset, java.lang.String... command)
-
exec
public static java.lang.String exec(java.lang.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()andgetBuffer(), 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
-
feedInput
private static void feedInput(java.lang.Process process, java.lang.String input, java.lang.String charset) throws java.io.IOException- Throws:
java.io.IOException
-
nomNomNom
private static java.lang.Integer nomNomNom(java.lang.Process process, OutputHandler stdOut, OutputHandler stdErr, java.lang.String charset)
-
getRawCmdStr
public java.lang.String getRawCmdStr()
-
getBuffer
public java.lang.String getBuffer()
Returns any output buffered from the process' stdout or stderr, depending on ifwithStdOutBuffered()and/orwithStdErrBuffered()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 java.lang.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(java.io.File dir)
Sets this process' working directory.- Parameters:
dir- this process' working directory- Returns:
- this object for chaining
-
withArg
public ProcessRunner withArg(java.lang.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(java.lang.String name, java.lang.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 namevalue- the variable value- Returns:
- this object for chaining
- See Also:
ProcessBuilder,System.getenv()
-
withCharset
public ProcessRunner withCharset(java.lang.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 bygetBuffer().- Returns:
- this object for chaining
-
withStdErrBuffered
public ProcessRunner withStdErrBuffered()
Sets this process' stdout stream to be stored in the buffer accessible bygetBuffer().- 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(java.lang.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
-
-