Package com.diffplug.spotless
Class ProcessRunner
java.lang.Object
com.diffplug.spotless.ProcessRunner
- All Implemented Interfaces:
AutoCloseable
Shelling out to a process is harder than it ought to be in Java.
If you don't read stdout and stderr on their own threads, you risk
deadlock on a clogged buffer.
ProcessRunner allocates two threads specifically for the purpose of flushing stdout and stderr to buffers. These threads will remain alive until the ProcessRunner is closed, so it is especially useful for repeated calls to an external process.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclass
A long-running process that can be waited for.static class
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Creates a process with the given arguments, the given byte array is written to stdin immediately.Creates a process with the given arguments, the given byte array is written to stdin immediately.Creates a process with the given arguments, the given byte array is written to stdin immediately.Creates a process with the given arguments.Creates a process with the given arguments.Executes the given shell command (usingcmd
on windows andsh
on unix).Executes the given shell command (usingcmd
on windows andsh
on unix).shellWinUnix
(String cmdWin, String cmdUnix) Executes the given shell command (usingcmd
on windows andsh
on unix).start
(File cwd, Map<String, String> environment, byte[] stdin, boolean redirectErrorStream, List<String> args) Creates a process with the given arguments, the given byte array is written to stdin immediately.Creates a process with the given arguments, the given byte array is written to stdin immediately.static ProcessRunner
usingRingBuffersOfCapacity
(int limit)
-
Constructor Details
-
ProcessRunner
public ProcessRunner()
-
-
Method Details
-
usingRingBuffersOfCapacity
-
shell
Executes the given shell command (usingcmd
on windows andsh
on unix).- Throws:
IOException
InterruptedException
-
shellWinUnix
public ProcessRunner.Result shellWinUnix(String cmdWin, String cmdUnix) throws IOException, InterruptedException Executes the given shell command (usingcmd
on windows andsh
on unix).- Throws:
IOException
InterruptedException
-
shellWinUnix
public ProcessRunner.Result shellWinUnix(@Nullable File cwd, @Nullable Map<String, String> environment, String cmdWin, String cmdUnix) throws IOException, InterruptedExceptionExecutes the given shell command (usingcmd
on windows andsh
on unix).- Throws:
IOException
InterruptedException
-
exec
Creates a process with the given arguments.- Throws:
IOException
InterruptedException
-
exec
public ProcessRunner.Result exec(@Nullable byte[] stdin, String... args) throws IOException, InterruptedException Creates a process with the given arguments, the given byte array is written to stdin immediately.- Throws:
IOException
InterruptedException
-
exec
Creates a process with the given arguments.- Throws:
IOException
InterruptedException
-
exec
public ProcessRunner.Result exec(@Nullable byte[] stdin, List<String> args) throws IOException, InterruptedException Creates a process with the given arguments, the given byte array is written to stdin immediately.- Throws:
IOException
InterruptedException
-
exec
public ProcessRunner.Result exec(@Nullable File cwd, @Nullable Map<String, String> environment, @Nullable byte[] stdin, List<String> args) throws IOException, InterruptedExceptionCreates a process with the given arguments, the given byte array is written to stdin immediately.- Throws:
IOException
InterruptedException
-
start
public ProcessRunner.LongRunningProcess start(@Nullable File cwd, @Nullable Map<String, String> environment, @Nullable byte[] stdin, List<String> args) throws IOExceptionCreates a process with the given arguments, the given byte array is written to stdin immediately.
Delegates tostart(File, Map, byte[], boolean, List)
withfalse
forredirectErrorStream
.- Throws:
IOException
-
start
public ProcessRunner.LongRunningProcess start(@Nullable File cwd, @Nullable Map<String, String> environment, @Nullable byte[] stdin, boolean redirectErrorStream, List<String> args) throws IOExceptionCreates a process with the given arguments, the given byte array is written to stdin immediately.
The process is not waited for, so the caller is responsible for callingProcessRunner.LongRunningProcess.waitFor()
(if needed).
To dispose thisProcessRunner
instance, either callclose()
orProcessRunner.LongRunningProcess.close()
. Afterclose()
orProcessRunner.LongRunningProcess.close()
has been called, thisProcessRunner
instance must not be used anymore.- Throws:
IOException
-
close
public void close()- Specified by:
close
in interfaceAutoCloseable
-