Package rife.bld.extension.tools
Class ProcessExecutor
java.lang.Object
rife.bld.extension.tools.ProcessExecutor
Generic process executor with timeout, I/O control, and output capture.
Framework-agnostic utility that can be composed by any extension. Handles process tree cleanup and stream management for Windows compatibility.
- Since:
- 1.0
- Author:
- Erik C. Thauvin
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordResult of a process execution. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncommand()Returns the mutable command and arguments list.Sets the command and arguments to be executed, replacing any previously configured command.command(Collection<String> args) Sets the command and arguments to be executed, replacing any previously configured command.env()Returns the mutable environment variables map.Adds an environment variable for the command.Adds environment variables for the command.execute()Executes the command and returns the result.booleanReturns whether the child process inherits the I/O streams of the current JVM.inheritIO(boolean inheritIO) Configures whether the child process should inherit the I/O streams of the current JVM.outputConsumer(Consumer<String> consumer) Sets a consumer to receive output lines as they arrive.inttimeout()Returns the command timeout in seconds.timeout(int timeout) Configure the command timeout in seconds.workDir()Returns the working directory.Configures the working directory.Configures the working directory.Configures the working directory.
-
Field Details
-
COMMAND_NOT_VALID
- See Also:
-
-
Constructor Details
-
ProcessExecutor
public ProcessExecutor()
-
-
Method Details
-
command
Sets the command and arguments to be executed, replacing any previously configured command.- Parameters:
args- one or more arguments, must not be null or contain null/empty elements- Returns:
- this instance
- Throws:
NullPointerException- if args is nullIllegalArgumentException- if args contains null or empty elements
-
command
Returns the mutable command and arguments list.- Returns:
- the command list, never null
-
command
Sets the command and arguments to be executed, replacing any previously configured command.- Parameters:
args- the list of arguments, must not be null or contain null/empty elements- Returns:
- this instance
- Throws:
NullPointerException- if args is nullIllegalArgumentException- if args contains null or empty elements
-
env
Adds an environment variable for the command.- Parameters:
name- the variable name, must not be nullvalue- the variable value, must not be null- Returns:
- this instance
- Throws:
NullPointerException- if name or value is null
-
env
Adds environment variables for the command.- Parameters:
vars- the map of environment variables, must not be null- Returns:
- this instance
- Throws:
NullPointerException- if vars is null
-
env
Returns the mutable environment variables map.- Returns:
- the environment map, never null
-
execute
Executes the command and returns the result.- Returns:
- the process result containing exit code and captured output
- Throws:
IOException- if the process cannot be startedInterruptedException- if the thread is interrupted while waitingIllegalStateException- if no command is set, the working directory is invalid, or bothinheritIO()andoutputConsumer(Consumer)are configured
-
inheritIO
Configures whether the child process should inherit the I/O streams of the current JVM.When
true, the child process uses the same stdin, stdout, and stderr as the current Java process. Output is not captured;ProcessExecutor.ProcessResult.output()will return an empty string in this mode.When
false(the default), stdout and stderr are merged and captured. Stdin receives EOF.Cannot be used with
outputConsumer(Consumer).- Parameters:
inheritIO-trueto inherit I/O,falseto capture output- Returns:
- this instance
-
inheritIO
public boolean inheritIO()Returns whether the child process inherits the I/O streams of the current JVM.- Returns:
trueif I/O is inherited,falseif output is captured
-
outputConsumer
Sets a consumer to receive output lines as they arrive.Only effective when
inheritIO()isfalse. The consumer is called from a background thread. Setting this implies output should be captured, not inherited.- Parameters:
consumer- the output consumer, or null to disable- Returns:
- this instance
-
timeout
Configure the command timeout in seconds.- Parameters:
timeout- the timeout, must be greater than 0- Returns:
- this instance
- Throws:
IllegalArgumentException- if timeout is less than or equal to 0
-
timeout
public int timeout()Returns the command timeout in seconds.- Returns:
- the timeout
-
workDir
Configures the working directory.- Parameters:
dir- the directory, must not be null- Returns:
- this instance
- Throws:
NullPointerException- if dir is null
-
workDir
Configures the working directory.- Parameters:
dir- the directory, must not be null- Returns:
- this instance
- Throws:
NullPointerException- if dir is null
-
workDir
Configures the working directory.- Parameters:
dir- the directory path, must not be null or empty- Returns:
- this instance
- Throws:
IllegalArgumentException- if dir is null or empty
-
workDir
Returns the working directory.- Returns:
- the directory, or null if not set
-