Package com.storedobject.common
Class SystemProcess
java.lang.Object
com.storedobject.common.SystemProcess
The SystemProcess class facilitates the execution of system-level commands within
a Java application. It allows the user to configure the command, environment variables,
and working directory for the process, and provides mechanisms to capture the process
output, error, and exit value.
This class supports chaining and modification of commands, managing input/output streams,
and exception handling for errors encountered during the execution of the system process.
Users can retrieve the output and errors generated by the process, as well as the process
exit value, to determine the outcome of the command execution.
- Author:
- Syam
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new SystemProcess object with an empty command.SystemProcess
(String command) Instantiates a SystemProcess object with a specified command to execute. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addCommand
(String command) Appends the specified command to the existing command list.void
execute()
Executes a system-level command with the pre-configured options including the command, environment variables, and working directory.void
Executes the given system command by setting it as the current command and then invoking the execution process.Retrieves the current system command as a list of strings.Retrieves the current working directory for the process execution.String[]
Retrieves the environment variables associated with the system process.getError()
Retrieves the error message generated during the execution of the process.int
Retrieves the exit value of the last executed system process.Retrieves the output produced by the executed system process.void
setCommand
(String command) Sets the command to be executed by this process.void
setDirectory
(File directory) Sets the working directory for the process to be executed.void
setEnvironment
(String[] environment) Sets the environment variables for the system process.
-
Constructor Details
-
SystemProcess
public SystemProcess()Constructs a new SystemProcess object with an empty command. This constructor initializes the internal state of the SystemProcess, allowing for further configuration such as setting commands, environment variables, or working directory before execution. -
SystemProcess
Instantiates a SystemProcess object with a specified command to execute.- Parameters:
command
- the initial system command to be executed; can be modified later.
-
-
Method Details
-
setCommand
Sets the command to be executed by this process. The command is stored internally as a list of strings, allowing modifications and additions to the command string at a later time.- Parameters:
command
- the initial command to be executed by the process. Cannot be null.
-
addCommand
Appends the specified command to the existing command list. This method allows the user to extend or modify the current command being constructed for execution.- Parameters:
command
- the command string to append to the existing command
-
getCommand
-
execute
-
execute
Executes a system-level command with the pre-configured options including the command, environment variables, and working directory. This method captures the standard output and error streams of the executed process and determines its exit value. Upon execution, the standard output and error streams are handled asynchronously by theStringCollector
utility, which gathers the output and errors into corresponding strings. The exit value of the process is retrieved and stored for later usage. If an exception is encountered while reading from the output or error streams, the same is thrown from this method.- Throws:
Exception
- if an exception occurs during the execution of the process or while reading the output/error streams
-
getExitValue
public int getExitValue()Retrieves the exit value of the last executed system process. The exit value is typically used to determine the success or failure of the executed command. A value of 0 usually indicates success, while non-zero values indicate errors or abnormal termination.- Returns:
- the exit value of the last executed process, or Integer.MIN_VALUE if the process has not been executed yet.
-
getOutput
Retrieves the output produced by the executed system process. The output is the standard output stream (stdout) captured during the execution of the process.- Returns:
- a String containing the standard output of the executed process. If the process has not been executed or no output was produced, it may return null.
-
getError
Retrieves the error message generated during the execution of the process.- Returns:
- the error output of the process as a String, or null if no error occurred.
-
getEnvironment
Retrieves the environment variables associated with the system process.- Returns:
- An array of strings representing the environment variables, or null if no environment variables are set.
-
setEnvironment
Sets the environment variables for the system process. These environment variables will be passed to the underlying process when it is executed.- Parameters:
environment
- an array of strings, where each string represents an environment variable in the format "key=value". If null, the process will inherit the environment of the current Java process.
-
getDirectory
Retrieves the current working directory for the process execution.- Returns:
- The directory where the process will be executed, or null if no directory has been specified.
-
setDirectory
Sets the working directory for the process to be executed. The specified directory must be a valid directory, or else the directory will not be updated.- Parameters:
directory
- the working directory to be set for the process. If null or not a valid directory, this method does nothing.
-