Package gw.util.process
Class ProcessRunner
- java.lang.Object
-
- gw.util.process.ProcessRunner
-
public class ProcessRunner extends Object
-
-
Constructor Summary
Constructors Constructor Description ProcessRunner(String... command)
ProcessRunner(List<String> command)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description 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).static String
exec(String... command)
static String
execWithCharset(String charset, String... command)
String
getBuffer()
Returns any output buffered from the process' stdout or stderr, depending on ifwithStdOutBuffered()
and/orwithStdErrBuffered()
were used.Integer
getExitCode()
Returns the process' exit code, if it finished.String
getRawCmdStr()
ProcessRunner
input(String input)
Sets the text to be directed into this process' stdin.ProcessRunner
withArg(String arg)
Adds an argument to the command.ProcessRunner
withCharset(String cs)
Sets the charset with which to write to this process' input and read its output.ProcessRunner
withCMD()
The process built up will used CMD.EXE if this is a windows platform.ProcessRunner
withEcho()
Sets this process' output to be displayed the parent process' stdout and stderr.ProcessRunner
withEnvironmentVariable(String name, String value)
Adds a name-value pair into this process' environment.ProcessRunner
withStdErrBuffered()
Sets this process' stdout stream to be stored in the buffer accessible bygetBuffer()
.ProcessRunner
withStdErrHandler(OutputHandler stdErrHandler)
Adds a block to handle lines output this process' stderr.ProcessRunner
withStdOutBuffered()
Sets this process' stdout stream to be stored in the buffer accessible bygetBuffer()
.ProcessRunner
withStdOutHandler(OutputHandler stdOutHandler)
Adds a block to handle lines output this process' stdout.ProcessRunner
withWorkingDirectory(File dir)
Sets this process' working directory.
-
-
-
Method Detail
-
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
-
getRawCmdStr
public String getRawCmdStr()
-
getBuffer
public 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 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 namevalue
- 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 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(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
-
-