Package gw.util.process
Class ProcessRunner
java.lang.Object
gw.util.process.ProcessRunner
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate OutputBuffer
private boolean
private boolean
private String
private boolean
private Integer
private String
private List<OutputHandler>
private List<OutputHandler>
private boolean
private File
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionexec()
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 String
static String
execWithCharset
(String charset, String... command) private static void
Returns any output buffered from the process' stdout or stderr, depending on ifwithStdOutBuffered()
and/orwithStdErrBuffered()
were used.Returns the process' exit code, if it finished.Sets the text to be directed into this process' stdin.private static Integer
nomNomNom
(Process process, OutputHandler stdOut, OutputHandler stdErr, String charset) Adds an argument to the command.withCharset
(String cs) Sets the charset with which to write to this process' input and read its output.withCMD()
The process built up will used CMD.EXE if this is a windows platform.withEcho()
Sets this process' output to be displayed the parent process' stdout and stderr.withEnvironmentVariable
(String name, String value) Adds a name-value pair into this process' environment.Sets this process' stdout stream to be stored in the buffer accessible bygetBuffer()
.withStdErrHandler
(OutputHandler stdErrHandler) Adds a block to handle lines output this process' stderr.Sets this process' stdout stream to be stored in the buffer accessible bygetBuffer()
.withStdOutHandler
(OutputHandler stdOutHandler) Adds a block to handle lines output this process' stdout.withWorkingDirectory
(File dir) Sets this process' working directory.
-
Field Details
-
_rawCmd
-
_buffer
-
_exitCode
-
_withCMD
private boolean _withCMD -
_workingDirectory
-
_env
-
_echo
private boolean _echo -
_input
-
_bufferStdOut
private boolean _bufferStdOut -
_bufferStdErr
private boolean _bufferStdErr -
_stdOutHandlers
-
_stdErrHandlers
-
_charset
-
-
Constructor Details
-
ProcessRunner
-
ProcessRunner
-
-
Method Details
-
execWithCharset
-
exec
-
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
- Throws:
IOException
-
nomNomNom
private static Integer nomNomNom(Process process, OutputHandler stdOut, OutputHandler stdErr, String charset) -
getRawCmdStr
-
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
Returns the process' exit code, if it finished.- Returns:
- the exit code, or null if the process never completed
-
withWorkingDirectory
Sets this process' working directory.- Parameters:
dir
- this process' working directory- Returns:
- this object for chaining
-
withArg
Adds an argument to the command.- Parameters:
arg
- the command line argument to add- Returns:
- this object for chaining
-
withEnvironmentVariable
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:
-
withCharset
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
Sets this process' stdout stream to be stored in the buffer accessible bygetBuffer()
.- Returns:
- this object for chaining
-
withStdErrBuffered
Sets this process' stdout stream to be stored in the buffer accessible bygetBuffer()
.- Returns:
- this object for chaining
-
withEcho
Sets this process' output to be displayed the parent process' stdout and stderr.- Returns:
- this object for chaining
-
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
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
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
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
-