Class ProcessExecutor
Framework-agnostic utility that can be composed by any extension. Handles process tree cleanup and stream management for Windows compatibility.
This class is not thread-safe. Configure and execute from a single thread.
- Since:
- 1.0
- Author:
- Erik C. Thauvin
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordResult of a process execution. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final longDefault timeout in seconds. -
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.longtimeout()Returns the command timeout in seconds.timeout(long 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
-
DEFAULT_TIMEOUT_SECONDS
public static final long DEFAULT_TIMEOUT_SECONDSDefault timeout in seconds.- 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. The first element must be a non-blank program name.- Returns:
- this instance
- Throws:
NullPointerException- if args is nullIllegalArgumentException- if args contains null/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. The first element must be a non-blank program name.- Returns:
- this instance
- Throws:
NullPointerException- if args is nullIllegalArgumentException- if args contains null/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.Exceptions thrown by the consumer are silently ignored to protect the output reader thread. Ensure the consumer handles its own errors if reliable delivery is required.
- Parameters:
consumer- the output consumer, or null to disable- Returns:
- this instance
-
timeout
Configure the command timeout in seconds.Use a negative value to disable the timeout and wait indefinitely.
A value of 0 is invalid as it would cause the process to fail immediately if not completed instantly.
Default:
DEFAULT_TIMEOUT_SECONDS- Parameters:
timeout- the timeout in seconds; use negative value for no timeout- Returns:
- this instance
- Throws:
IllegalArgumentException- if timeout is 0
-
timeout
public long 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
-