Class ProcessRunner

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    public class ProcessRunner
    extends java.lang.Object
    implements java.lang.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.
    • Constructor Summary

      Constructors 
      Constructor Description
      ProcessRunner()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      ProcessRunner.Result exec​(byte[] stdin, java.lang.String... args)
      Creates a process with the given arguments, the given byte array is written to stdin immediately.
      ProcessRunner.Result exec​(byte[] stdin, java.util.List<java.lang.String> args)
      Creates a process with the given arguments, the given byte array is written to stdin immediately.
      ProcessRunner.Result exec​(java.io.File cwd, java.util.Map<java.lang.String,​java.lang.String> environment, byte[] stdin, java.util.List<java.lang.String> args)
      Creates a process with the given arguments, the given byte array is written to stdin immediately.
      ProcessRunner.Result exec​(java.lang.String... args)
      Creates a process with the given arguments.
      ProcessRunner.Result exec​(java.util.List<java.lang.String> args)
      Creates a process with the given arguments.
      ProcessRunner.Result shell​(java.lang.String cmd)
      Executes the given shell command (using cmd on windows and sh on unix).
      ProcessRunner.Result shellWinUnix​(java.io.File cwd, java.util.Map<java.lang.String,​java.lang.String> environment, java.lang.String cmdWin, java.lang.String cmdUnix)
      Executes the given shell command (using cmd on windows and sh on unix).
      ProcessRunner.Result shellWinUnix​(java.lang.String cmdWin, java.lang.String cmdUnix)
      Executes the given shell command (using cmd on windows and sh on unix).
      ProcessRunner.LongRunningProcess start​(java.io.File cwd, java.util.Map<java.lang.String,​java.lang.String> environment, byte[] stdin, boolean redirectErrorStream, java.util.List<java.lang.String> args)
      Creates a process with the given arguments, the given byte array is written to stdin immediately.
      ProcessRunner.LongRunningProcess start​(java.io.File cwd, java.util.Map<java.lang.String,​java.lang.String> environment, byte[] stdin, java.util.List<java.lang.String> args)
      Creates a process with the given arguments, the given byte array is written to stdin immediately.
      static ProcessRunner usingRingBuffersOfCapacity​(int limit)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ProcessRunner

        public ProcessRunner()
    • Method Detail

      • usingRingBuffersOfCapacity

        public static ProcessRunner usingRingBuffersOfCapacity​(int limit)
      • shell

        public ProcessRunner.Result shell​(java.lang.String cmd)
                                   throws java.io.IOException,
                                          java.lang.InterruptedException
        Executes the given shell command (using cmd on windows and sh on unix).
        Throws:
        java.io.IOException
        java.lang.InterruptedException
      • shellWinUnix

        public ProcessRunner.Result shellWinUnix​(java.lang.String cmdWin,
                                                 java.lang.String cmdUnix)
                                          throws java.io.IOException,
                                                 java.lang.InterruptedException
        Executes the given shell command (using cmd on windows and sh on unix).
        Throws:
        java.io.IOException
        java.lang.InterruptedException
      • shellWinUnix

        public ProcessRunner.Result shellWinUnix​(@Nullable
                                                 java.io.File cwd,
                                                 @Nullable
                                                 java.util.Map<java.lang.String,​java.lang.String> environment,
                                                 java.lang.String cmdWin,
                                                 java.lang.String cmdUnix)
                                          throws java.io.IOException,
                                                 java.lang.InterruptedException
        Executes the given shell command (using cmd on windows and sh on unix).
        Throws:
        java.io.IOException
        java.lang.InterruptedException
      • exec

        public ProcessRunner.Result exec​(java.lang.String... args)
                                  throws java.io.IOException,
                                         java.lang.InterruptedException
        Creates a process with the given arguments.
        Throws:
        java.io.IOException
        java.lang.InterruptedException
      • exec

        public ProcessRunner.Result exec​(@Nullable
                                         byte[] stdin,
                                         java.lang.String... args)
                                  throws java.io.IOException,
                                         java.lang.InterruptedException
        Creates a process with the given arguments, the given byte array is written to stdin immediately.
        Throws:
        java.io.IOException
        java.lang.InterruptedException
      • exec

        public ProcessRunner.Result exec​(java.util.List<java.lang.String> args)
                                  throws java.io.IOException,
                                         java.lang.InterruptedException
        Creates a process with the given arguments.
        Throws:
        java.io.IOException
        java.lang.InterruptedException
      • exec

        public ProcessRunner.Result exec​(@Nullable
                                         byte[] stdin,
                                         java.util.List<java.lang.String> args)
                                  throws java.io.IOException,
                                         java.lang.InterruptedException
        Creates a process with the given arguments, the given byte array is written to stdin immediately.
        Throws:
        java.io.IOException
        java.lang.InterruptedException
      • exec

        public ProcessRunner.Result exec​(@Nullable
                                         java.io.File cwd,
                                         @Nullable
                                         java.util.Map<java.lang.String,​java.lang.String> environment,
                                         @Nullable
                                         byte[] stdin,
                                         java.util.List<java.lang.String> args)
                                  throws java.io.IOException,
                                         java.lang.InterruptedException
        Creates a process with the given arguments, the given byte array is written to stdin immediately.
        Throws:
        java.io.IOException
        java.lang.InterruptedException
      • start

        public ProcessRunner.LongRunningProcess start​(@Nullable
                                                      java.io.File cwd,
                                                      @Nullable
                                                      java.util.Map<java.lang.String,​java.lang.String> environment,
                                                      @Nullable
                                                      byte[] stdin,
                                                      java.util.List<java.lang.String> args)
                                               throws java.io.IOException
        Creates a process with the given arguments, the given byte array is written to stdin immediately.
        Delegates to start(File, Map, byte[], boolean, List) with false for redirectErrorStream.
        Throws:
        java.io.IOException
      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable