- java.lang.Object
-
- net.morimekta.io.proc.SubProcessRunner
-
public class SubProcessRunner extends Object
Helper class to run a subprocess and handle the process' input and output streams. This can be done to run a subprocess as an interactive shell, like this:
It's main design is to handle simple commands with it's output. Setting the sub-process' input (part ofSubProcessRunner runner = new SubProcessRunner(); runner.setOut(System.out); runner.setErr(System.err); runner.exec(System.in, "sh", "-c" "cat file | less");
exec(InputStream, String...)
params) will act the same as controlling the programs input, whether it asks for key-presses or piped input.
-
-
Constructor Summary
Constructors Constructor Description SubProcessRunner()
Create a SubProcessRunner instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
exec(InputStream in, String... command)
Execute command with specified input.int
exec(String... command)
Execute command with no input.void
setDeadlineFlushMs(long deadlineFlushMs)
Set deadline in millis for the output handlers to finish after the program has exited.void
setDeadlineMs(long deadlineMs)
Set deadline in millis for the program to finish.void
setErr(OutputStream err)
Set output stream to receive the programs standard error stream.void
setOut(OutputStream out)
Set output stream to receive the programs standard output stream.void
setRuntime(Runtime runtime)
Set the runtime implementation used to run processes.void
setThreadFactory(ThreadFactory threadFactory)
void
setWorkingDir(Path workingDir)
Set the working dir where the program should be run.
-
-
-
Method Detail
-
setRuntime
public void setRuntime(Runtime runtime)
Set the runtime implementation used to run processes.- Parameters:
runtime
- The runtime implementation.
-
setThreadFactory
public void setThreadFactory(ThreadFactory threadFactory)
- Parameters:
threadFactory
- Set the thread factory used to generate local threads used for handling IO piping.
-
setOut
public void setOut(OutputStream out)
Set output stream to receive the programs standard output stream.- Parameters:
out
- The output stream
-
setErr
public void setErr(OutputStream err)
Set output stream to receive the programs standard error stream.- Parameters:
err
- The output stream
-
setWorkingDir
public void setWorkingDir(Path workingDir)
Set the working dir where the program should be run.- Parameters:
workingDir
- The working dir path.
-
setDeadlineMs
public void setDeadlineMs(long deadlineMs)
Set deadline in millis for the program to finish. If the program does not finish in the assigned deadline, it will be forcefully terminated and anIOException
will be thrown fromexec(String...)
.- Parameters:
deadlineMs
- The deadline in MS.
-
setDeadlineFlushMs
public void setDeadlineFlushMs(long deadlineFlushMs)
Set deadline in millis for the output handlers to finish after the program has exited. If the streams are not fully handled within that time, theexec(String...)
method will throw anIOException
. This is per default 1s, which will be the deadline of 0 is set.- Parameters:
deadlineFlushMs
- The deadline in MS.
-
exec
public int exec(String... command) throws IOException
Execute command with no input. This will immediately close the stream to the process, so if it tries to read it will not block, but exit.- Parameters:
command
- The command to be executed.- Returns:
- The exit code of the program.
- Throws:
IOException
- If the program execution failed.
-
exec
public int exec(InputStream in, String... command) throws IOException
Execute command with specified input.- Parameters:
in
- The input stream to read input from.command
- The command to be executed.- Returns:
- The exit code of the program.
- Throws:
IOException
- If the program execution failed.
-
-