Class CommandRunner

  • All Implemented Interfaces:
    java.lang.Runnable

    public class CommandRunner
    extends java.lang.Thread
    Facilitates the execution of an external command and the collection of its output
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      protected class  CommandRunner.StreamGobbler
      Constantly reads an input stream and captures its content.
      • Nested classes/interfaces inherited from class java.lang.Thread

        java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
    • Constructor Summary

      Constructors 
      Constructor Description
      CommandRunner​(java.lang.String[] command)
      Creates a CommandRunner to run a command.
      CommandRunner​(java.lang.String[] command, java.lang.String stdin)
      Creates a CommandRunner to run a command.
      CommandRunner​(java.util.List<java.lang.String> command, java.lang.String stdin)
      Creates a CommandRunner to run a command.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      byte[] getBytes()
      Gets the contents of stdout sent by the command as an array of bytes
      int getErrorCode()
      Gets the return code of the command.
      java.lang.String getString()
      Gets the contents of stdout sent by the command as a string
      void run()  
      static byte[] runAndGet​(java.lang.String[] command, java.lang.String inputs)  
      static byte[] runAndGet​(java.lang.String command, java.lang.String inputs)  
      void stopCommand()
      Interrupts the execution of the command
      • Methods inherited from class java.lang.Thread

        activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, suspend, toString, yield
      • Methods inherited from class java.lang.Object

        equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • m_command

        protected java.lang.String[] m_command
      • m_stdin

        protected java.lang.String m_stdin
      • m_stop

        protected volatile boolean m_stop
      • m_errorCode

        protected int m_errorCode
    • Constructor Detail

      • CommandRunner

        public CommandRunner​(java.util.List<java.lang.String> command,
                             java.lang.String stdin)
        Creates a CommandRunner to run a command.
        Parameters:
        command - The command to run
        stdin - If not set to null, this string will be sent to the stdin of the command being run
      • CommandRunner

        public CommandRunner​(java.lang.String[] command,
                             java.lang.String stdin)
        Creates a CommandRunner to run a command.
        Parameters:
        command - The command to run
        stdin - If not set to null, this string will be sent to the stdin of the command being run
      • CommandRunner

        public CommandRunner​(java.lang.String[] command)
        Creates a CommandRunner to run a command.
        Parameters:
        command - The command to run
    • Method Detail

      • runAndGet

        public static byte[] runAndGet​(java.lang.String[] command,
                                       java.lang.String inputs)
      • runAndGet

        public static byte[] runAndGet​(java.lang.String command,
                                       java.lang.String inputs)
      • run

        public void run()
        Specified by:
        run in interface java.lang.Runnable
        Overrides:
        run in class java.lang.Thread
      • getBytes

        public byte[] getBytes()
        Gets the contents of stdout sent by the command as an array of bytes
        Returns:
        The contents of stdout
      • getString

        public java.lang.String getString()
        Gets the contents of stdout sent by the command as a string
        Returns:
        The contents of stdout
      • getErrorCode

        public int getErrorCode()
        Gets the return code of the command. Generally 0 indicates that everything was OK; a non-zero value indicates an error.
        Returns:
        The return code
      • stopCommand

        public void stopCommand()
        Interrupts the execution of the command