Package com.cedarsoftware.util
Class Executor
java.lang.Object
com.cedarsoftware.util.Executor
A utility class for executing system commands and capturing their output.
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
Example Usage:
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.
-
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.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
-
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
-
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
-
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
-
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
-
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
-
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
-
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
-