Class CommandLineUtils


  • public abstract class CommandLineUtils
    extends java.lang.Object
    Author:
    Trygve Laugstøl
    • Constructor Detail

      • CommandLineUtils

        public CommandLineUtils()
    • Method Detail

      • executeCommandLine

        public static int executeCommandLine​(Commandline cl,
                                             java.io.InputStream systemIn,
                                             StreamConsumer systemOut,
                                             StreamConsumer systemErr,
                                             int timeoutInSeconds)
                                      throws CommandLineException
        Parameters:
        cl - the command line to execute
        systemIn - the input to read from, must be thread safe
        systemOut - a consumer that receives output, must be thread safe
        systemErr - a consumer that receives system error stream output, must be thread safe
        timeoutInSeconds - positive integer to specify timeout, zero and negative integers for no timeout
        Returns:
        a return value, see Process.exitValue()
        Throws:
        CommandLineException - or CommandLineTimeOutException if time out occurs
      • executeCommandLine

        public static int executeCommandLine​(Commandline cl,
                                             java.io.InputStream systemIn,
                                             StreamConsumer systemOut,
                                             StreamConsumer systemErr,
                                             int timeoutInSeconds,
                                             java.lang.Runnable runAfterProcessTermination)
                                      throws CommandLineException
        Parameters:
        cl - the command line to execute
        systemIn - the input to read from, must be thread safe
        systemOut - a consumer that receives output, must be thread safe
        systemErr - a consumer that receives system error stream output, must be thread safe
        timeoutInSeconds - positive integer to specify timeout, zero and negative integers for no timeout
        runAfterProcessTermination - optional callback to run after the process terminated or the the timeout was exceeded, but before waiting on the stream feeder and pumpers to finish
        Returns:
        a return value, see Process.exitValue()
        Throws:
        CommandLineException - or CommandLineTimeOutException if time out occurs
      • executeCommandLine

        public static int executeCommandLine​(Commandline cl,
                                             java.io.InputStream systemIn,
                                             StreamConsumer systemOut,
                                             StreamConsumer systemErr,
                                             int timeoutInSeconds,
                                             java.lang.Runnable runAfterProcessTermination,
                                             java.nio.charset.Charset streamCharset)
                                      throws CommandLineException
        Parameters:
        cl - the command line to execute
        systemIn - the input to read from, must be thread safe
        systemOut - a consumer that receives output, must be thread safe
        systemErr - a consumer that receives system error stream output, must be thread safe
        timeoutInSeconds - positive integer to specify timeout, zero and negative integers for no timeout
        runAfterProcessTermination - optional callback to run after the process terminated or the the timeout was exceeded, but before waiting on the stream feeder and pumpers to finish
        streamCharset - charset to use for reading streams
        Returns:
        a return value, see Process.exitValue()
        Throws:
        CommandLineException - or CommandLineTimeOutException if time out occurs
      • executeCommandLineAsCallable

        public static CommandLineCallable executeCommandLineAsCallable​(Commandline cl,
                                                                       java.io.InputStream systemIn,
                                                                       StreamConsumer systemOut,
                                                                       StreamConsumer systemErr,
                                                                       int timeoutInSeconds,
                                                                       java.lang.Runnable runAfterProcessTermination)
                                                                throws CommandLineException
        Immediately forks a process, returns a callable that will block until process is complete.
        Parameters:
        cl - the command line to execute
        systemIn - the input to read from, must be thread safe
        systemOut - a consumer that receives output, must be thread safe
        systemErr - a consumer that receives system error stream output, must be thread safe
        timeoutInSeconds - positive integer to specify timeout, zero and negative integers for no timeout
        runAfterProcessTermination - optional callback to run after the process terminated or the the timeout was
        Returns:
        a CommandLineCallable that provides the process return value, see Process.exitValue(). "call" must be called on this to be sure the forked process has terminated, no guarantees is made about any internal state before after the completion of the call statements
        Throws:
        CommandLineException - or CommandLineTimeOutException if time out occurs
      • executeCommandLineAsCallable

        public static CommandLineCallable executeCommandLineAsCallable​(Commandline cl,
                                                                       java.io.InputStream systemIn,
                                                                       StreamConsumer systemOut,
                                                                       StreamConsumer systemErr,
                                                                       int timeoutInSeconds,
                                                                       java.lang.Runnable runAfterProcessTermination,
                                                                       java.nio.charset.Charset streamCharset)
                                                                throws CommandLineException
        Immediately forks a process, returns a callable that will block until process is complete.
        Parameters:
        cl - the command line to execute
        systemIn - the input to read from, must be thread safe
        systemOut - a consumer that receives output, must be thread safe
        systemErr - a consumer that receives system error stream output, must be thread safe
        timeoutInSeconds - positive integer to specify timeout, zero and negative integers for no timeout
        runAfterProcessTermination - optional callback to run after the process terminated or the the timeout was
        streamCharset - charset to use for reading streams
        Returns:
        a CommandLineCallable that provides the process return value, see Process.exitValue(). "call" must be called on this to be sure the forked process has terminated, no guarantees is made about any internal state before after the completion of the call statements
        Throws:
        CommandLineException - or CommandLineTimeOutException if time out occurs
      • getSystemEnvVars

        @Deprecated
        public static java.util.Properties getSystemEnvVars()
        Deprecated.
        use System#getenv()
        Gets the shell environment variables for this process. Note that the returned mapping from variable names to values will always be case-sensitive regardless of the platform, i.e. getSystemEnvVars().get("path") and getSystemEnvVars().get("PATH") will in general return different values. However, on platforms with case-insensitive environment variables like Windows, all variable names will be normalized to upper case.
        Returns:
        the shell environment variables, can be empty but never null
      • getSystemEnvVars

        @Deprecated
        public static java.util.Properties getSystemEnvVars​(boolean caseSensitive)
        Deprecated.
        use System#getenv()
        Return the shell environment variables. If caseSensitive == true, then envar keys will all be upper-case.
        Parameters:
        caseSensitive - whether environment variable keys should be treated case-sensitively
        Returns:
        properties object of (possibly modified) envar keys mapped to their values
      • translateCommandline

        public static java.lang.String[] translateCommandline​(java.lang.String toProcess)
                                                       throws CommandLineException
        Parameters:
        toProcess - the command line to translate
        Returns:
        the array of translated parts
        Throws:
        CommandLineException - in case of unbalanced quotes
      • toString

        public static java.lang.String toString​(java.lang.String... line)
        Parameters:
        line - the lines
        Returns:
        the concatenated lines, quoted and escaped, separated by spaces