public class ProcessRunner extends Object
| Constructor and Description |
|---|
ProcessRunner(List<String> command) |
ProcessRunner(String... command) |
| Modifier and Type | Method and 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 if
withStdOutBuffered() and/or withStdErrBuffered() 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 by
getBuffer(). |
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 by
getBuffer(). |
ProcessRunner |
withStdOutHandler(OutputHandler stdOutHandler)
Adds a block to handle lines output this process' stdout.
|
ProcessRunner |
withWorkingDirectory(File dir)
Sets this process' working directory.
|
public ProcessRunner(String... command)
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()
public String getRawCmdStr()
public String getBuffer()
withStdOutBuffered() and/or withStdErrBuffered() were used.
If a buffer was desired and the process printed nothing out, an empty string is
returned.public Integer getExitCode()
public ProcessRunner withWorkingDirectory(File dir)
dir - this process' working directorypublic ProcessRunner withArg(String arg)
arg - the command line argument to addpublic ProcessRunner withEnvironmentVariable(String name, String value)
name - the variable namevalue - the variable valueProcessBuilder,
System.getenv()public ProcessRunner withCharset(String cs)
cs - the charset to usepublic ProcessRunner withStdOutBuffered()
getBuffer().public ProcessRunner withStdErrBuffered()
getBuffer().public ProcessRunner withEcho()
public ProcessRunner input(String input)
input - the text to direct into stdinpublic ProcessRunner withCMD()
public ProcessRunner withStdErrHandler(OutputHandler stdErrHandler)
stdErrHandler - handler that will be called with every line of output to stderrpublic ProcessRunner withStdOutHandler(OutputHandler stdOutHandler)
stdOutHandler - handler that will be called with every line of output to stdoutCopyright © 2020. All rights reserved.