Class Executor
This class provides a convenient wrapper around Java's Runtime.exec(String)
methods,
capturing both standard output and standard error streams. It handles stream management
and process cleanup automatically.
Features:
- Executes system commands with various parameter combinations
- Captures stdout and stderr output
- Supports environment variables
- Supports working directory specification
- Non-blocking output handling
Security Configuration
Due to the inherent security risks of executing arbitrary system commands, Executor is disabled by default and must be explicitly enabled. This is a security-first approach that requires intentional opt-in for command execution capabilities.
Security control can be configured via system property:
executor.enabled=false
— Enable/disable all command execution (default: false)
Security Features
- Disabled by Default: Command execution is disabled unless explicitly enabled
- Explicit Opt-in: Users must consciously enable this dangerous functionality
- Complete Control: Single property controls all execution methods
- Clear Error Messages: SecurityExceptions provide instructions on how to enable
Security Warning
⚠️ WARNING: This class executes arbitrary system commands with the privileges of the JVM process. Only enable with trusted input and in environments where command execution is necessary.
Usage Example
// Enable command execution (required)
System.setProperty("executor.enabled", "true");
// Now command execution will work
Executor exec = new Executor();
int exitCode = exec.exec("ls -l");
String output = exec.getOut();
Breaking Change Notice
⚠️ BREAKING CHANGE: As of this version, Executor is disabled by default.
Existing code that uses Executor will need to add System.setProperty("executor.enabled", "true")
before using any Executor methods.
Basic Usage:
// First, enable command execution (required)
System.setProperty("executor.enabled", "true");
// Then use Executor normally
Executor exec = new Executor();
int exitCode = exec.exec("ls -l");
String output = exec.getOut(); // Get stdout
String errors = exec.getError(); // Get stderr
- Author:
- John DeRegnaucourt ([email protected])
Copyright (c) Cedar Software LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
License
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.Thread Safety: Instances of this class are not thread safe. Create a new
Executor
per command execution or synchronize externally if sharing across threads.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionint
Executes a command using the system's runtime environment.int
Executes a command array using the system's runtime environment.int
Executes a command array with specified environment variables.int
Executes a command array with specified environment variables and working directory.int
Executes a command with specified environment variables.int
Executes a command with specified environment variables and working directory.Execute the supplied command line using the platform shell.Execute the specified command array.Execute a command array with environment variables.Execute a command array with optional environment and working directory.Execute a command with environment variables.Execute a command with optional environment and working directory.getError()
Returns the content written to standard error by the last executed command.getOut()
Returns the content written to standard output by the last executed command.
-
Constructor Details
-
Executor
public Executor()
-
-
Method Details
-
execute
Execute the supplied command line using the platform shell.- Parameters:
command
- command to execute- Returns:
- result of the execution
- Throws:
SecurityException
- if command execution is disabled
-
execute
Execute the specified command array.- Parameters:
cmdarray
- command and arguments- Returns:
- result of the execution
- Throws:
SecurityException
- if command execution is disabled
-
execute
Execute a command with environment variables.- Parameters:
command
- command line to runenvp
- environment variables, may benull
- Returns:
- result of the execution
- Throws:
SecurityException
- if command execution is disabled
-
execute
Execute a command array with environment variables.- Parameters:
cmdarray
- command and argumentsenvp
- environment variables, may benull
- Returns:
- result of the execution
- Throws:
SecurityException
- if command execution is disabled
-
execute
Execute a command with optional environment and working directory.- Parameters:
command
- command line to runenvp
- environment variables ornull
dir
- working directory, may benull
- Returns:
- result of the execution
- Throws:
SecurityException
- if command execution is disabled
-
execute
Execute a command array with optional environment and working directory.- Parameters:
cmdarray
- command and argumentsenvp
- environment variables ornull
dir
- working directory, may benull
- Returns:
- result of the execution
- Throws:
SecurityException
- if command execution is disabled
-
exec
Executes a command using the system's runtime environment.- Parameters:
command
- the command to execute- Returns:
- the exit value of the process (0 typically indicates success), or -1 if an error occurred starting the process
- Throws:
SecurityException
- if command execution is disabled
-
exec
Executes a command array using the system's runtime environment.This version allows commands with arguments to be specified as separate array elements, avoiding issues with argument quoting and escaping.
- Parameters:
cmdarray
- array containing the command and its arguments- Returns:
- the exit value of the process (0 typically indicates success), or -1 if an error occurred starting the process
- Throws:
SecurityException
- if command execution is disabled
-
exec
Executes a command with specified environment variables.- Parameters:
command
- the command to executeenvp
- array of strings, each element of which has environment variable settings in format name=value, or null if the subprocess should inherit the environment of the current process- Returns:
- the exit value of the process (0 typically indicates success), or -1 if an error occurred starting the process
- Throws:
SecurityException
- if command execution is disabled
-
exec
Executes a command array with specified environment variables.- Parameters:
cmdarray
- array containing the command and its argumentsenvp
- array of strings, each element of which has environment variable settings in format name=value, or null if the subprocess should inherit the environment of the current process- Returns:
- the exit value of the process (0 typically indicates success), or -1 if an error occurred starting the process
- Throws:
SecurityException
- if command execution is disabled
-
exec
Executes a command with specified environment variables and working directory.- Parameters:
command
- the command to executeenvp
- array of strings, each element of which has environment variable settings in format name=value, or null if the subprocess should inherit the environment of the current processdir
- the working directory of the subprocess, or null if the subprocess should inherit the working directory of the current process- Returns:
- the exit value of the process (0 typically indicates success), or -1 if an error occurred starting the process
- Throws:
SecurityException
- if command execution is disabled
-
exec
Executes a command array with specified environment variables and working directory.- Parameters:
cmdarray
- array containing the command and its argumentsenvp
- array of strings, each element of which has environment variable settings in format name=value, or null if the subprocess should inherit the environment of the current processdir
- the working directory of the subprocess, or null if the subprocess should inherit the working directory of the current process- Returns:
- the exit value of the process (0 typically indicates success), or -1 if an error occurred starting the process
- Throws:
SecurityException
- if command execution is disabled
-
getError
Returns the content written to standard error by the last executed command.- Returns:
- the stderr output as a string, or null if no command has been executed
-
getOut
Returns the content written to standard output by the last executed command.- Returns:
- the stdout output as a string, or null if no command has been executed
-