public class ProcessUtils extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
ProcessUtils.ExecutionResult
Parameter object that encapsulates the result of a process execution.
|
Modifier and Type | Field and Description |
---|---|
static java.nio.charset.Charset |
CONSOLE_CHARSET
Character set used for process I/O.
|
static java.lang.String |
MONO_COMMAND
Command (path or name) to mono, to execute .NET programs on non-windows
machines.
|
Constructor and Description |
---|
ProcessUtils() |
Modifier and Type | Method and Description |
---|---|
static java.lang.ProcessBuilder |
createDotNetProcessBuilder(java.lang.String... arguments)
Creates a
ProcessBuilder for executing a .net assembly. |
static ProcessUtils.ExecutionResult |
execute(java.lang.ProcessBuilder builder)
Executes a process in a thread-safe way.
|
static ProcessUtils.ExecutionResult |
execute(java.lang.ProcessBuilder builder,
java.lang.String input)
Executes a process in a thread-safe way.
|
static ProcessUtils.ExecutionResult |
execute(java.lang.ProcessBuilder builder,
java.lang.String input,
int timeOut)
Executes a process in a thread-safe way.
|
static ProcessUtils.ExecutionResult |
execute(java.lang.ProcessBuilder builder,
java.lang.String input,
int timeOut,
boolean collectOutputStreamContent)
Executes a process in a thread-safe way.
|
static ProcessUtils.ExecutionResult |
execute(java.lang.String[] completeArguments)
Executes a process in a thread-safe way.
|
static ProcessUtils.ExecutionResult |
execute(java.lang.String[] completeArguments,
java.lang.String input)
Executes a process in a thread-safe way.
|
static ProcessUtils.ExecutionResult |
executeDotNet(java.lang.String[] completeArguments)
Executes a .net program in a thread-safe way.
|
static <T extends java.lang.Exception> |
executeOrThrow(java.lang.ProcessBuilder builder,
java.lang.String input,
int timeout,
java.util.function.BiFunction<java.lang.String,java.lang.Throwable,T> exceptionConstructor)
Runs the given process builder with the given input.
|
public static final java.lang.String MONO_COMMAND
public static java.nio.charset.Charset CONSOLE_CHARSET
public ProcessUtils()
public static java.lang.ProcessBuilder createDotNetProcessBuilder(java.lang.String... arguments)
ProcessBuilder
for executing a .net assembly. If the
current OS is not Windows mono will be used for executing the assembly.public static ProcessUtils.ExecutionResult executeDotNet(java.lang.String[] completeArguments) throws java.io.IOException
java.io.IOException
public static ProcessUtils.ExecutionResult execute(java.lang.String[] completeArguments) throws java.io.IOException
completeArguments
- Array of command line arguments to start the processjava.io.IOException
public static ProcessUtils.ExecutionResult execute(java.lang.String[] completeArguments, java.lang.String input) throws java.io.IOException
completeArguments
- Array of command line arguments to start the processinput
- String that gets written to stdinjava.io.IOException
public static ProcessUtils.ExecutionResult execute(java.lang.ProcessBuilder builder) throws java.io.IOException
builder
- builder that gets executedjava.io.IOException
public static ProcessUtils.ExecutionResult execute(java.lang.ProcessBuilder builder, java.lang.String input) throws java.io.IOException
builder
- builder that gets executedinput
- String that gets written to stdinjava.io.IOException
public static ProcessUtils.ExecutionResult execute(java.lang.ProcessBuilder builder, java.lang.String input, int timeOut) throws java.io.IOException
builder
- builder that gets executedinput
- String that gets written to stdin (may be null).timeOut
- the number to seconds to wait for the process. If this runs
longer, the process is killed. Passing a value of 0 or less makes
the method wait forever (until the process finishes normally). To
find out whether the process was killed, query
ProcessUtils.ExecutionResult.isNormalTermination()
.java.io.IOException
public static ProcessUtils.ExecutionResult execute(java.lang.ProcessBuilder builder, java.lang.String input, int timeOut, boolean collectOutputStreamContent) throws java.io.IOException
builder
- builder that gets executedinput
- String that gets written to stdin (may be null).timeOut
- the number to seconds to wait for the process. If this runs
longer, the process is killed. Passing a value of 0 or less makes
the method wait forever (until the process finishes normally). To
find out whether the process was killed, query
ProcessUtils.ExecutionResult.isNormalTermination()
.collectOutputStreamContent
- boolean that indicates if the content from the stderr and stdout
shall be collected. False is useful if the content is not needed
and / or if the content would cause an OutOfMemoryException.java.io.IOException
public static <T extends java.lang.Exception> ProcessUtils.ExecutionResult executeOrThrow(java.lang.ProcessBuilder builder, java.lang.String input, int timeout, java.util.function.BiFunction<java.lang.String,java.lang.Throwable,T> exceptionConstructor) throws T extends java.lang.Exception
builder
- The process builder to execute.input
- The input to the program.timeout
- How many seconds to wait before terminating the process. If this
is less than 1, no timeout is applied.exceptionConstructor
- Constructs the exception that is thrown in case something goes
wrong. The first argument is a descriptive message, the second is
either null
(in case of a timeout or non-zero exit
code) or the exception that caused the failure. This parameter is
compatible with most exception constructors, e.g.
ConQATException::new
.T
- An exception constructed with the given constructor. Thrown if
the process fails to run and return a zero exit code.T extends java.lang.Exception