- java.lang.Object
-
- net.automatalib.common.util.process.ProcessUtil
-
public final class ProcessUtil extends Object
Utility class for invoking system processes.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Process
buildProcess(String[] commandLine, @Nullable Reader input, @Nullable Consumer<String> stdOutConsumer, @Nullable Consumer<String> stdErrConsumer)
Builds and starts a system process for the given set of command line arguments.static int
invokeProcess(String[] commandLine)
Runs the given set of command line arguments as a system process and returns the exit value of the spawned process.static int
invokeProcess(String[] commandLine, Reader input)
Runs the given set of command line arguments as a system process and returns the exit value of the spawned process.static int
invokeProcess(String[] commandLine, Reader input, Consumer<String> consumer)
Runs the given set of command line arguments as a system process and returns the exit value of the spawned process.static int
invokeProcess(String[] commandLine, Consumer<String> consumer)
Runs the given set of command line arguments as a system process and returns the exit value of the spawned process.static int
invokeProcess(List<String> commandLine)
Runs the given set of command line arguments as a system process and returns the exit value of the spawned process.static int
invokeProcess(List<String> commandLine, Reader input)
Runs the given set of command line arguments as a system process and returns the exit value of the spawned process.static int
invokeProcess(List<String> commandLine, Reader input, Consumer<String> consumer)
Runs the given set of command line arguments as a system process and returns the exit value of the spawned process.static int
invokeProcess(List<String> commandLine, Consumer<String> consumer)
Runs the given set of command line arguments as a system process and returns the exit value of the spawned process.
-
-
-
Method Detail
-
invokeProcess
public static int invokeProcess(String[] commandLine) throws IOException, InterruptedException
Runs the given set of command line arguments as a system process and returns the exit value of the spawned process. Discards any output of the process.- Parameters:
commandLine
- the list of command line arguments to run- Returns:
- the exit code of the process
- Throws:
IOException
- if an exception occurred while reading the process' outputsInterruptedException
- if an exception occurred during process exception
-
invokeProcess
public static int invokeProcess(List<String> commandLine) throws IOException, InterruptedException
Runs the given set of command line arguments as a system process and returns the exit value of the spawned process. Discards any output of the process.- Parameters:
commandLine
- the list of command line arguments to run- Returns:
- the exit code of the process
- Throws:
IOException
- if an exception occurred while reading the process' outputsInterruptedException
- if an exception occurred during process exception
-
invokeProcess
public static int invokeProcess(String[] commandLine, Reader input) throws IOException, InterruptedException
Runs the given set of command line arguments as a system process and returns the exit value of the spawned process. Additionally, allows to supply an input stream to the invoked program. Discards any output of the process.- Parameters:
commandLine
- the list of command line arguments to runinput
- the input passed to the program- Returns:
- the exit code of the process
- Throws:
IOException
- if an exception occurred while reading the process' outputs, or writing the process' inputsInterruptedException
- if an exception occurred during process exception
-
invokeProcess
public static int invokeProcess(List<String> commandLine, Reader input) throws IOException, InterruptedException
Runs the given set of command line arguments as a system process and returns the exit value of the spawned process. Additionally, allows to supply an input stream to the invoked program. Discards any output of the process.- Parameters:
commandLine
- the list of command line arguments to runinput
- the input passed to the program- Returns:
- the exit code of the process
- Throws:
IOException
- if an exception occurred while reading the process' outputs, or writing the process' inputsInterruptedException
- if an exception occurred during process exception
-
invokeProcess
public static int invokeProcess(String[] commandLine, Consumer<String> consumer) throws IOException, InterruptedException
Runs the given set of command line arguments as a system process and returns the exit value of the spawned process. Outputs of the process (both normal and error) are passed to theconsumer
.- Parameters:
commandLine
- the list of command line arguments to runconsumer
- the consumer for the program's output- Returns:
- the exit code of the process
- Throws:
IOException
- if an exception occurred while reading the process' outputsInterruptedException
- if an exception occurred during process exception
-
invokeProcess
public static int invokeProcess(List<String> commandLine, Consumer<String> consumer) throws IOException, InterruptedException
Runs the given set of command line arguments as a system process and returns the exit value of the spawned process. Outputs of the process (both normal and error) are passed to theconsumer
.- Parameters:
commandLine
- the list of command line arguments to runconsumer
- the consumer for the program's output- Returns:
- the exit code of the process
- Throws:
IOException
- if an exception occurred while reading the process' outputsInterruptedException
- if an exception occurred during process exception
-
invokeProcess
public static int invokeProcess(String[] commandLine, Reader input, Consumer<String> consumer) throws IOException, InterruptedException
Runs the given set of command line arguments as a system process and returns the exit value of the spawned process. Additionally, allows to supply an input stream to the invoked program. Outputs of the process (both normal and error) are passed to theconsumer
.- Parameters:
commandLine
- the list of command line arguments to runinput
- the input passed to the programconsumer
- the consumer for the program's output- Returns:
- the exit code of the process
- Throws:
IOException
- if an exception occurred while reading the process' outputs, or writing the process' inputsInterruptedException
- if an exception occurred during process exception
-
invokeProcess
public static int invokeProcess(List<String> commandLine, Reader input, Consumer<String> consumer) throws IOException, InterruptedException
Runs the given set of command line arguments as a system process and returns the exit value of the spawned process. Additionally, allows to supply an input stream to the invoked program. Outputs of the process (both normal and error) are passed to theconsumer
.- Parameters:
commandLine
- the list of command line arguments to runinput
- the input passed to the programconsumer
- the consumer for the program's output- Returns:
- the exit code of the process
- Throws:
IOException
- if an exception occurred while reading the process' outputs, or writing the process' inputsInterruptedException
- if an exception occurred during process exception
-
buildProcess
public static Process buildProcess(String[] commandLine, @Nullable Reader input, @Nullable Consumer<String> stdOutConsumer, @Nullable Consumer<String> stdErrConsumer) throws IOException
Builds and starts a system process for the given set of command line arguments. Additionally, allows to supply an input stream to the invoked program, as well as independent consumers for the process' standard and error output.The consumers for the process' outputs run in separate threads, preventing potential deadlock scenarios where client code waits for the process' termination (e.g.
Process.waitFor()
) which is blocked by full system buffers.- Parameters:
commandLine
- the list of command line arguments to runinput
- the input passed to the program, maybe benull
if the process expects no inputstdOutConsumer
- the consumer for the programs outputstdErrConsumer
- the consumer for the programs output- Returns:
- the reference to the running process
- Throws:
IOException
- if an exception occurred while reading the process outputs
-
-