Class CommandExec
java.lang.Object
com.pnfsoftware.jeb.util.concurrent.CommandExec
A wrapper around
Process to execute short commands, in a blocking or non-blocking way
with or without a timeout, and retrieve the exit code and generated output(s).
A lower-level alternative that offers access to the streams (including stdin) is
ProcessWrapper.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreate an object that will execute commands in blocking mode, without timeout.CommandExec(long timeoutMs, boolean redirectErrToOut) Create an object that will execute commands. -
Method Summary
Modifier and TypeMethodDescriptionbooleanExecute a command.booleanexecute(Collection<String> cmdlist) Execute a command.byte[]getError()Retrieve the command execution's error bytes, if execution completed.Retrieve the command execution's exit value, if execution completed.byte[]Retrieve the command execution's output bytes, if execution completed.
-
Constructor Details
-
CommandExec
public CommandExec()Create an object that will execute commands in blocking mode, without timeout. -
CommandExec
public CommandExec(long timeoutMs, boolean redirectErrToOut) Create an object that will execute commands.- Parameters:
timeoutMs- timeout for the command execution, in milliseconds:- zero: blocking, without timeout
- positive value: blocking, and target will be killed when timeout has elapsed
- negative value: non-blocking,
executereturns true immediately; subsequent calls togetOutput()return null
redirectErrToOut- if true, the error stream will be redirected to the output stream; this flag is disregarded if the timeout is negative
-
-
Method Details
-
getExitValue
Retrieve the command execution's exit value, if execution completed. Never available in non-blocking mode.- Returns:
- the exit value or null
-
getOutput
public byte[] getOutput()Retrieve the command execution's output bytes, if execution completed. Never available in non-blocking mode.- Returns:
- the bytes or null
-
getError
public byte[] getError()Retrieve the command execution's error bytes, if execution completed. Never available in non-blocking mode.- Returns:
- the bytes or null
-
execute
Execute a command. The method may block, depending on the timeout value provided to the constructor.- Parameters:
cmdlist- the command tokens (command and arguments), eg:["ls", "-l", "*.java"]- Returns:
- true in non-blocking mode or if execution completed, false otherwise
-
execute
Execute a command. The method may block, depending on the timeout value provided to the constructor.- Parameters:
cmdarray- the command tokens (command and arguments), eg:["ls", "-l", "*.java"]- Returns:
- true in non-blocking mode or if execution completed, false otherwise
-