Class ExternalCommand


  • public class ExternalCommand
    extends java.lang.Object
    This class encapsulates a java Process to handle properly output and error and preventing potential deadlocks. The idea is that the command is executed and you can get the error or output. Simple to use. Should not be used for big outputs because they are buffered internally.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String JDK_HOME_DIR  
      static org.apache.logging.log4j.Logger LOG  
      static java.lang.String MODULE  
    • Constructor Summary

      Constructors 
      Constructor Description
      ExternalCommand​(java.lang.ProcessBuilder processBuilder)
      Constructor
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static ExternalCommand create​(java.lang.String... commands)
      Creates an external process from the command.
      static ExternalCommand create​(java.util.List<java.lang.String> commands)
      Creates an external process from the command.
      void destroy()  
      static ExternalCommand execute​(java.io.File workingDirectory, java.lang.String command, java.lang.String... args)
      Executes the external command in the given working directory and waits for it to be finished.
      static ExternalCommand executeWithTimeout​(java.io.File workingDirectory, java.lang.String command, long timeout, java.lang.String... args)
      Executes the external command in the given working directory and waits (until timeout is elapsed) for it to be finished.
      static ExternalCommand executeWithTimeoutWithEnv​(java.io.File workingDirectory, java.lang.String command, long timeout, java.util.Map<java.lang.String,​java.lang.String> env, java.lang.String... args)  
      int exitValue()  
      java.util.Map<java.lang.String,​java.lang.String> getEnvironment()  
      byte[] getError()  
      byte[] getOutput()  
      boolean getRedirectErrorStream()  
      java.lang.String getStringError()
      Returns the error as a string.
      java.lang.String getStringError​(java.lang.String encoding)
      Returns the error as a string.
      java.lang.String getStringError​(java.nio.charset.Charset encoding)
      Returns the error as a string.
      java.lang.String getStringOutput()
      Returns the output as a string.
      java.lang.String getStringOutput​(java.lang.String encoding)
      Returns the output as a string.
      java.lang.String getStringOutput​(java.nio.charset.Charset encoding)
      Returns the output as a string.
      java.io.File getWorkingDirectory()  
      boolean isTimedOut()  
      void setRedirectErrorStream​(boolean redirectErrorStream)  
      void setTimedOut​(boolean val)  
      void setWorkingDirectory​(java.io.File directory)  
      void start()
      After creating the command, you have to start it...
      static ExternalCommand start​(java.lang.String... commands)
      Creates an external process from the command.
      int waitFor()
      Properly waits until everything is complete: joins on the thread that reads the output, joins on the thread that reads the error and finally wait for the process to be finished.
      int waitFor​(long timeout)
      Properly waits until everything is complete: joins on the thread that reads the output, joins on the thread that reads the error and finally wait for the process to be finished.
      int waitFor​(long timeout, java.util.concurrent.TimeUnit unit)
      Properly waits until everything is complete: joins on the thread that reads the output, joins on the thread that reads the error and finally wait for the process to be finished.
      • Methods inherited from class java.lang.Object

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

      • JDK_HOME_DIR

        public static final java.lang.String JDK_HOME_DIR
      • MODULE

        public static final java.lang.String MODULE
      • LOG

        public static final org.apache.logging.log4j.Logger LOG
    • Constructor Detail

      • ExternalCommand

        public ExternalCommand​(java.lang.ProcessBuilder processBuilder)
        Constructor
    • Method Detail

      • start

        public void start()
                   throws java.io.IOException
        After creating the command, you have to start it...
        Throws:
        java.io.IOException
      • getEnvironment

        public java.util.Map<java.lang.String,​java.lang.String> getEnvironment()
        See Also:
        ProcessBuilder
      • getWorkingDirectory

        public java.io.File getWorkingDirectory()
        See Also:
        ProcessBuilder
      • setWorkingDirectory

        public void setWorkingDirectory​(java.io.File directory)
        See Also:
        ProcessBuilder
      • getRedirectErrorStream

        public boolean getRedirectErrorStream()
        See Also:
        ProcessBuilder
      • setRedirectErrorStream

        public void setRedirectErrorStream​(boolean redirectErrorStream)
        See Also:
        ProcessBuilder
      • setTimedOut

        public void setTimedOut​(boolean val)
      • isTimedOut

        public boolean isTimedOut()
      • getOutput

        public byte[] getOutput()
                         throws java.lang.InterruptedException
        Throws:
        java.lang.InterruptedException
      • getError

        public byte[] getError()
                        throws java.lang.InterruptedException
        Throws:
        java.lang.InterruptedException
      • getStringOutput

        public java.lang.String getStringOutput​(java.lang.String encoding)
                                         throws java.lang.InterruptedException,
                                                java.io.UnsupportedEncodingException
        Returns the output as a string.
        Parameters:
        encoding -
        Returns:
        encoded string
        Throws:
        java.lang.InterruptedException
        java.io.UnsupportedEncodingException
      • getStringOutput

        public java.lang.String getStringOutput​(java.nio.charset.Charset encoding)
                                         throws java.lang.InterruptedException
        Returns the output as a string.
        Parameters:
        encoding -
        Returns:
        encoded string
        Throws:
        java.lang.InterruptedException
        java.io.UnsupportedEncodingException
      • getStringOutput

        public java.lang.String getStringOutput()
                                         throws java.lang.InterruptedException
        Returns the output as a string. Uses encoding "UTF-8".
        Returns:
        utf8 encoded string
        Throws:
        java.lang.InterruptedException
      • getStringError

        public java.lang.String getStringError​(java.nio.charset.Charset encoding)
                                        throws java.lang.InterruptedException
        Returns the error as a string.
        Parameters:
        encoding -
        Returns:
        error as string
        Throws:
        java.lang.InterruptedException
      • getStringError

        public java.lang.String getStringError​(java.lang.String encoding)
                                        throws java.lang.InterruptedException,
                                               java.io.UnsupportedEncodingException
        Returns the error as a string.
        Parameters:
        encoding -
        Returns:
        error as string
        Throws:
        java.lang.InterruptedException
        java.io.UnsupportedEncodingException
      • getStringError

        public java.lang.String getStringError()
                                        throws java.lang.InterruptedException
        Returns the error as a string. Uses encoding "UTF-8".
        Returns:
        error as string
        Throws:
        java.lang.InterruptedException
      • waitFor

        public int waitFor()
                    throws java.lang.InterruptedException
        Properly waits until everything is complete: joins on the thread that reads the output, joins on the thread that reads the error and finally wait for the process to be finished.
        Returns:
        the status code of the process.
        Throws:
        java.lang.InterruptedException
      • waitFor

        public int waitFor​(long timeout,
                           java.util.concurrent.TimeUnit unit)
                    throws java.lang.InterruptedException,
                           java.util.concurrent.TimeoutException,
                           java.io.IOException
        Properly waits until everything is complete: joins on the thread that reads the output, joins on the thread that reads the error and finally wait for the process to be finished. If the process has not completed before the timeout, throws a TimeoutException
        Returns:
        the status code of the process.
        Throws:
        java.util.concurrent.TimeoutException
        java.lang.InterruptedException
        java.io.IOException
      • waitFor

        public int waitFor​(long timeout)
                    throws java.lang.InterruptedException,
                           java.util.concurrent.TimeoutException,
                           java.io.IOException
        Properly waits until everything is complete: joins on the thread that reads the output, joins on the thread that reads the error and finally wait for the process to be finished. If the process has not completed before the timeout, throws a TimeoutException
        Returns:
        the status code of the process.
        Throws:
        java.util.concurrent.TimeoutException
        java.lang.InterruptedException
        java.io.IOException
      • exitValue

        public int exitValue()
      • destroy

        public void destroy()
      • create

        public static ExternalCommand create​(java.lang.String... commands)
        Creates an external process from the command. It is not started and you have to call start on it!
        Parameters:
        commands - the command to execute
        Returns:
        the process
      • create

        public static ExternalCommand create​(java.util.List<java.lang.String> commands)
        Creates an external process from the command. It is not started and you have to call start on it!
        Parameters:
        commands - the command to execute
        Returns:
        the process
      • start

        @CheckReturnValue
        public static ExternalCommand start​(java.lang.String... commands)
                                     throws java.io.IOException
        Creates an external process from the command. The command is executed.
        Parameters:
        commands - the commands to execute
        Returns:
        the process
        Throws:
        java.io.IOException - if there is an error
      • execute

        @CheckReturnValue
        public static ExternalCommand execute​(java.io.File workingDirectory,
                                              java.lang.String command,
                                              java.lang.String... args)
                                       throws java.io.IOException,
                                              java.lang.InterruptedException
        Executes the external command in the given working directory and waits for it to be finished.
        Parameters:
        workingDirectory - the root directory from where to run the command
        command - the command to execute (should be relative to the working directory
        args - the arguments to the command
        Returns:
        the process
        Throws:
        java.io.IOException
        java.lang.InterruptedException
      • executeWithTimeout

        @CheckReturnValue
        public static ExternalCommand executeWithTimeout​(java.io.File workingDirectory,
                                                         java.lang.String command,
                                                         long timeout,
                                                         java.lang.String... args)
                                                  throws java.io.IOException,
                                                         java.lang.InterruptedException
        Executes the external command in the given working directory and waits (until timeout is elapsed) for it to be finished.
        Parameters:
        workingDirectory - the root directory from where to run the command
        command - the command to execute (should be relative to the working directory
        timeout - the maximum amount of time to wait for this external command (in ms). If this value is less than or equal to 0, timeout is ignored
        args - the arguments to the command
        Returns:
        the process
        Throws:
        java.io.IOException
        java.lang.InterruptedException
      • executeWithTimeoutWithEnv

        @CheckReturnValue
        public static ExternalCommand executeWithTimeoutWithEnv​(java.io.File workingDirectory,
                                                                java.lang.String command,
                                                                long timeout,
                                                                java.util.Map<java.lang.String,​java.lang.String> env,
                                                                java.lang.String... args)
                                                         throws java.io.IOException,
                                                                java.lang.InterruptedException
        Throws:
        java.io.IOException
        java.lang.InterruptedException