java.lang.Object
one.jpro.platform.utils.CommandRunner
Run processes based on command line arguments.
-
Constructor Summary
ConstructorsConstructorDescriptionCommandRunner(String... args) Initializes CommandRunner with default logger and command line arguments.CommandRunner(org.slf4j.Logger logger, @NotNull String... args) Initializes CommandRunner with specified logger and command line arguments. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a command line argument to the list of existing list of command line argumentsvoidAdds a varargs list of arguments to the existing list of command line of argumentsvoidaddArgs(@NotNull Collection<String> args) Adds a collection of arguments to the existing list of command line of argumentsvoidaddSecretArg(@Nullable String arg) Adds a command line argument to the list of existing list of command line arguments, marking it as secret argument, in order to avoid loggingvoidAdds a pair (key, value) to the environment map of the process.getCmd()Gets the last line of the output process.@Nullable StringGets the response of the process as single string.Gets the response of the process as list of lines.intRuns a process with a given set of command line arguments.intRuns a process with a given set of command line arguments, in a given working directory.Runs a process asynchronously with a given set of command line arguments.Runs a process asynchronously with a given set of command line arguments.Runs a process asynchronously with a given set of command line arguments, in a given working directory.Runs a process asynchronously with a given set of command line arguments, in a given working directory.booleanRuns a process with a given set of command line arguments within a given time frame.booleanRuns a process with a given set of command line arguments, in a given working directory, within a given time framevoidsetInteractive(boolean interactive) When set to true, it will enable user interaction during the process.voidsetPrintToConsole(boolean printToConsole) When set to true, it will enable the process output to be printed to the console.
-
Constructor Details
-
CommandRunner
Initializes CommandRunner with default logger and command line arguments.- Parameters:
args- command line arguments to be executed by the process.
-
CommandRunner
Initializes CommandRunner with specified logger and command line arguments.- Parameters:
logger- Logger instance for logging process activities.args- Command line arguments to be executed by the process.
-
-
Method Details
-
setInteractive
public void setInteractive(boolean interactive) When set to true, it will enable user interaction during the process. By default, is false- Parameters:
interactive- a boolean that sets the interactive mode
-
setPrintToConsole
public void setPrintToConsole(boolean printToConsole) When set to true, it will enable the process output to be printed to the console. By default, is false- Parameters:
printToConsole- a boolean that sets the print to console mode
-
addArg
Adds a command line argument to the list of existing list of command line arguments- Parameters:
arg- a string passed to the command line arguments
-
addSecretArg
Adds a command line argument to the list of existing list of command line arguments, marking it as secret argument, in order to avoid logging- Parameters:
arg- a string passed to the command line arguments
-
addArgs
Adds a varargs list of arguments to the existing list of command line of arguments- Parameters:
args- varargs list of arguments
-
addArgs
Adds a collection of arguments to the existing list of command line of arguments- Parameters:
args- a collection of arguments
-
getCmd
- Returns:
- the command line of arguments as a string
-
getCmdList
- Returns:
- the current list of command line arguments
-
addToEnv
Adds a pair (key, value) to the environment map of the process.- Parameters:
key- a string with the environmental variable namevalue- a string with the environmental variable value
-
run
Runs a process with a given set of command line arguments.- Parameters:
processName- the name of the process- Returns:
- 0 if the process ends successfully, non-zero values indicate a failure
- Throws:
IOException- if an I/O error occursInterruptedException- if the current thread is interrupted by another thread while it is waiting
-
run
public int run(@Nullable @Nullable String processName, @Nullable @Nullable File workingDirectory) throws IOException, InterruptedException Runs a process with a given set of command line arguments, in a given working directory.- Parameters:
processName- the name of the processworkingDirectory- a file with the working directory of the process- Returns:
- 0 if the process ends successfully, non-zero values indicate a failure
- Throws:
IOException- if an I/O error occursInterruptedException- if the current thread is interrupted by another thread while it is waiting
-
runAsync
Runs a process asynchronously with a given set of command line arguments. By default, it merges the output of the process.- Parameters:
processName- the name of the process- Returns:
- the
Processobject - Throws:
IOException- if an I/O error occurs
-
runAsync
public Process runAsync(@Nullable @Nullable String processName, @Nullable @Nullable File workingDirectory) throws IOException Runs a process asynchronously with a given set of command line arguments, in a given working directory. By default, it merges the output of the process.- Parameters:
processName- the name of the processworkingDirectory- a file with the working directory of the process- Returns:
- the
Processobject - Throws:
IOException- if an I/O error occurs
-
runAsync
public Process runAsync(@Nullable @Nullable String processName, boolean mergeOutput) throws IOException Runs a process asynchronously with a given set of command line arguments.- Parameters:
processName- the name of the processmergeOutput- a boolean that sets the merge output mode- Returns:
- the
Processobject - Throws:
IOException- if an I/O error occurs
-
runAsync
public Process runAsync(@Nullable @Nullable String processName, @Nullable @Nullable File workingDirectory, boolean mergeOutput) throws IOException Runs a process asynchronously with a given set of command line arguments, in a given working directory.- Parameters:
processName- the name of the processworkingDirectory- a file with the working directory of the processmergeOutput- a boolean that sets the merge output mode- Returns:
- the
Processobject - Throws:
IOException- if an I/O error occurs
-
runTimed
public boolean runTimed(@Nullable @Nullable String processName, long timeout) throws IOException, InterruptedException Runs a process with a given set of command line arguments within a given time frame.- Parameters:
processName- the name of the processtimeout- the maximum time allowed to run the process in seconds- Returns:
- true if the process ended successfully, false otherwise
- Throws:
IOException- if an I/O error occursInterruptedException- if the current thread is interrupted by another thread while it is waiting
-
runTimed
public boolean runTimed(@Nullable @Nullable String processName, @Nullable @Nullable File workingDirectory, long timeout) throws IOException, InterruptedException Runs a process with a given set of command line arguments, in a given working directory, within a given time frame- Parameters:
processName- the name of the processworkingDirectory- a file with the working directory of the processtimeout- the maximum time allowed to run the process- Returns:
- true if the process ended successfully, false otherwise
- Throws:
IOException- if an I/O error occursInterruptedException- if the current thread is interrupted by another thread while it is waiting
-
getResponse
Gets the response of the process as single string.- Returns:
- a single string with the whole output of the process
-
getResponses
Gets the response of the process as list of lines.- Returns:
- a list with all the lines of the output
-
getLastResponse
Gets the last line of the output process.- Returns:
- a string with the last line of the output
-