Package org.buildobjects.process
Class ProcBuilder
java.lang.Object
org.buildobjects.process.ProcBuilder
A builder to construct a new process. The process gets configured by the withXXX-methods and
spawned by the run() method
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionClears the environment before setting new variables.static StringStatic helper to filter a string through a processDeprecated.Use getCommandLine instead.Ignore the error status returned from this commandrun()Spawn the actual execution.static StringStatic helper to run a processAdds another argumentAdd multiple argswithErrorConsumer(StreamConsumer errorConsumer) Process the error output with given consumer objectwithErrorStream(OutputStream stderr) Redirecting the error output.withExitStatuses(int[] exitstatuses) Deprecated.Please use the variants with a set or vargs parameterswithExpectedExitStatuses(int... expectedExitStatuses) Define the valid exit status codes for the command Convenience method taking varargs.withExpectedExitStatuses(Set<Integer> expectedExitStatuses) Define the valid exit status codes for the commandwithInput(byte[] input) Supply the input as byte[]Supply the input as stringwithInputStream(InputStream stdin) Take the input for the program from a given InputStreamDisable timeout for the operation.withOutputConsumer(StreamConsumer outputConsumer) Process the standard output with the given consumer objectwithOutputStream(OutputStream stdout) Redirecting the standard output.withTimeoutMillis(long timeoutMillis) Specify a timeout for the operation.Add a variable to the process's environmentAdd multiple variables to the process's environmentwithWorkingDirectory(File directory) Override the wokring directory
-
Constructor Details
-
ProcBuilder
Creates a new ProcBuilder- Parameters:
command- The command to runargs- The command line arguments
-
-
Method Details
-
withArg
Adds another argument- Parameters:
arg- to add- Returns:
- this, for chaining
-
withOutputStream
Redirecting the standard output. If it is not redirected the output gets captured in memory and is available on the @see ProcResult- Parameters:
stdout- stream to redirect the output to. \- Returns:
- this, for chaining
-
withErrorStream
Redirecting the error output. If it is not redirected the output gets captured in memory and is available on the @see ProcResult- Parameters:
stderr- stream to redirect the output to. \- Returns:
- this, for chaining
-
withTimeoutMillis
Specify a timeout for the operation. If not specified the default is 5 seconds.- Parameters:
timeoutMillis- time that the process gets to run- Returns:
- this, for chaining
-
withNoTimeout
Disable timeout for the operation.- Returns:
- this, for chaining
-
withInputStream
Take the input for the program from a given InputStream- Parameters:
stdin- stream to read the input from- Returns:
- this, for chaining
-
withInput
Supply the input as string- Parameters:
input- the actual input- Returns:
- this, for chaining
-
withInput
Supply the input as byte[]- Parameters:
input- the actual input- Returns:
- this, for chaining
-
withWorkingDirectory
Override the wokring directory- Parameters:
directory- the working directory for the process- Returns:
- this, for chaining
-
withArgs
Add multiple args- Parameters:
args- the arguments add- Returns:
- this, for chaining
-
withExitStatuses
Deprecated.Please use the variants with a set or vargs parametersDefine the valid exit status codes for the command- Parameters:
exitstatuses- array containing the exit codes that are valid- Returns:
- the ProcBuilder object; permits chaining.
-
withExpectedExitStatuses
Define the valid exit status codes for the command- Parameters:
expectedExitStatuses- array containing the exit codes that are valid- Returns:
- the ProcBuilder object; permits chaining.
-
withExpectedExitStatuses
Define the valid exit status codes for the command Convenience method taking varargs.- Parameters:
expectedExitStatuses- varargs parameter containing the exit codes that are valid- Returns:
- the ProcBuilder object; permits chaining.
-
ignoreExitStatus
Ignore the error status returned from this command- Returns:
- the ProcBuilder object; permits chaining.
-
run
Spawn the actual execution. This will block until the process terminates.- Returns:
- the result of the successful execution
- Throws:
StartupException- if the process can't be startedTimeoutException- if the timeout kicked inExternalProcessFailureException- if the external process returned a non-null exit value
-
run
Static helper to run a process- Parameters:
cmd- the commandargs- the arguments- Returns:
- the standard output
- Throws:
StartupException- if the process can't be startedTimeoutException- if the timeout kicked inExternalProcessFailureException- if the external process returned a non-null exit value
-
filter
Static helper to filter a string through a process- Parameters:
input- the input to be fed into the processcmd- the commandargs- the arguments- Returns:
- the standard output
- Throws:
StartupException- if the process can't be startedTimeoutException- if the timeout kicked inExternalProcessFailureException- if the external process returned a non-null exit value
-
clearEnvironment
Clears the environment before setting new variables. -
withVar
Add a variable to the process's environment- Parameters:
var- variable namevalue- the value to be passed in- Returns:
- this, for chaining
-
withVars
Add multiple variables to the process's environment- Parameters:
vars- Map of variables to their respective values- Returns:
- this, for chaining
-
withOutputConsumer
Process the standard output with the given consumer object- Parameters:
outputConsumer- an object that defines how to process the standard output stream- Returns:
- this, for chaining
-
withErrorConsumer
Process the error output with given consumer object- Parameters:
errorConsumer- an object that defines how to process the error output stream- Returns:
- this, for chaining
-
getProcString
Deprecated.Use getCommandLine instead.- Returns:
- a string representation of the process invocation. This approximates the representation of this invocation in a shell. Note that the escaping of arguments is incomplete, it works only for whitespace. Fancy control characters are not replaced. Also, this returns a representation of the current state of the builder. If more arguments are added the process this representation will not represent the process that gets launched.
-
getCommandLine
- Returns:
- a string representation of the process invocation. This approximates the representation of this invocation in a shell. Note that the escaping of arguments is incomplete, it works only for whitespace. Fancy control characters are not replaced. Also, this returns a representation of the current state of the builder. If more arguments are added the process this representation will not represent the process that gets launched.
-