Package bm.util
Class Exec
java.lang.Object
bm.util.Exec
Somewhat simplified apis around
ProcessBuilder to run sub-processes.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordProvides standard and error output and the exit code of a process as strings after the process exits. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidexec(ProcessBuilder pb) Executes the process and waits for the termination.static intexec(ProcessBuilder pb, Consumer<Process> afterStart) Starts the process defined by the process builder, then applies the given consumer and then waits for the process to teminate.static voidCallsexec(List)after wrapping the arguments into a list.static voidSimplified API to start a process with default std(in|out|err) and wait for the outcome.static Exec.OutExecutes the command line and waits for the termination.static Exec.OutexecReadBoth(List<String> cmdline) Executes the command line and reads both, standard output and standard error to return it as part of the result.static Exec.OutexecReadStderr(List<String> cmdline) Executes the command line and reads standard error to return it as part of the result.static Exec.OutexecReadStdout(List<String> cmdline) Executes the command line and reads standard output to return it as part of the result.
-
Field Details
-
PATH_EMPTY
-
-
Constructor Details
-
Exec
public Exec()
-
-
Method Details
-
exec
Callsexec(List)after wrapping the arguments into a list. Intended for the simple cases with constant string arguments.- Parameters:
command- to runargs- command line parameters for the command- Throws:
BuildProblemException
-
exec
Simplified API to start a process with default std(in|out|err) and wait for the outcome.- Parameters:
cmdline- to execute. Consider usingStringListerto build up the argument list.- Throws:
BuildProblemException- if the process return status is not 0
-
execReadBoth
Executes the command line and reads both, standard output and standard error to return it as part of the result.- Throws:
BuildProblemException
-
execReadStdout
Executes the command line and reads standard output to return it as part of the result.- Throws:
BuildProblemException
-
execReadStderr
Executes the command line and reads standard error to return it as part of the result.- Throws:
BuildProblemException
-
exec
Executes the command line and waits for the termination. Depending on the parameters, standard output and error are either inherited, written to a file or captured and returned in the result.- Parameters:
cmdline- to executeout- ifnull, inherit stdout, if a validPath, send output there, if an emptyPath, return it as {#linkExec.Out.stdout()}err- likeoutbut for standard error- Returns:
- the standard and/or error output of the process and its exit code
- Throws:
BuildProblemException- if the process cannot be started or reading the output failed
-
exec
Executes the process and waits for the termination. If the exit cade is not 0, an exception is thrown- Parameters:
pb- to execute- Throws:
BuildProblemException- if the exit code of the process is not 0 are problems appear when starting the process
-
exec
public static int exec(ProcessBuilder pb, Consumer<Process> afterStart) throws BuildProblemException Starts the process defined by the process builder, then applies the given consumer and then waits for the process to teminate.- Parameters:
pb- defines the process to startafterStart- is called with the process object after the process was started. This allows, for example, to provide input for the process (if the builder prepared for it)- Returns:
- the exit code of the process
- Throws:
BuildProblemException
-