Class ProcessUtils


  • public class ProcessUtils
    extends Object
    A helper class for process related stuff.
    Author:
    fracpete (fracpete at waikato dot ac dot nz)
    • Constructor Detail

      • ProcessUtils

        public ProcessUtils()
    • Method Detail

      • getVirtualMachinePID

        public static long getVirtualMachinePID()
        Returns the PID of the virtual machine. Caution: it's a hack and can break anytime. Do NOT rely on it. Based on this blog entry.
        Returns:
        the PID or -1 in case of an error
      • execute

        public static com.github.fracpete.processoutput4j.output.CollectingProcessOutput execute​(String cmd)
                                                                                          throws Exception
        Executes the command and returns a result container.
        Parameters:
        cmd - the command to execute
        Returns:
        the results of the proces: exit code, stdout, stderr
        Throws:
        Exception - if something goes wrong
      • execute

        public static com.github.fracpete.processoutput4j.output.CollectingProcessOutput execute​(String cmd,
                                                                                                 PlaceholderDirectory cwd)
                                                                                          throws Exception
        Executes the command and returns a result container.
        Parameters:
        cmd - the command to execute
        cwd - the working directory for the command, null to ignore
        Returns:
        the results of the proces: exit code, stdout, stderr
        Throws:
        Exception - if something goes wrong
      • execute

        public static com.github.fracpete.processoutput4j.output.CollectingProcessOutput execute​(String cmd,
                                                                                                 HashMap<String,​String> env,
                                                                                                 PlaceholderDirectory cwd)
                                                                                          throws Exception
        Executes the command and returns a result container.
        Parameters:
        cmd - the command to execute
        env - the environment variables, null if to inherit current ones
        cwd - the working directory for the command, null to ignore
        Returns:
        the results of the proces: exit code, stdout, stderr
        Throws:
        Exception - if something goes wrong
      • execute

        public static com.github.fracpete.processoutput4j.output.CollectingProcessOutput execute​(String cmd,
                                                                                                 HashMap<String,​String> env,
                                                                                                 String input,
                                                                                                 PlaceholderDirectory cwd)
                                                                                          throws Exception
        Executes the command and returns a result container.
        Parameters:
        cmd - the command to execute
        env - the environment variables, null if to inherit current ones
        input - the input to write to the process
        cwd - the working directory for the command, null to ignore
        Returns:
        the results of the proces: exit code, stdout, stderr
        Throws:
        Exception - if something goes wrong
      • execute

        public static com.github.fracpete.processoutput4j.output.CollectingProcessOutput execute​(String cmd,
                                                                                                 String[] env,
                                                                                                 PlaceholderDirectory cwd)
                                                                                          throws Exception
        Executes the command and returns a result container.
        Parameters:
        cmd - the command to execute
        env - the environment variables, null if to inherit current ones
        cwd - the working directory for the command, null to ignore
        Returns:
        the results of the proces: exit code, stdout, stderr
        Throws:
        Exception - if something goes wrong
      • execute

        public static com.github.fracpete.processoutput4j.output.CollectingProcessOutput execute​(String[] cmd)
                                                                                          throws Exception
        Executes the commandline array and returns a result container.
        Parameters:
        cmd - the commandline array to execute
        Returns:
        the results of the proces: exit code, stdout, stderr
        Throws:
        Exception - if something goes wrong
      • execute

        public static com.github.fracpete.processoutput4j.output.CollectingProcessOutput execute​(String[] cmd,
                                                                                                 PlaceholderDirectory cwd)
                                                                                          throws Exception
        Executes the commandline array and returns a result container.
        Parameters:
        cmd - the commandline array to execute
        cwd - the working directory for the command, null to ignore
        Returns:
        the results of the proces: exit code, stdout, stderr
        Throws:
        Exception - if something goes wrong
      • execute

        public static com.github.fracpete.processoutput4j.output.CollectingProcessOutput execute​(String[] cmd,
                                                                                                 HashMap<String,​String> env,
                                                                                                 PlaceholderDirectory cwd)
                                                                                          throws Exception
        Executes the commandline array and returns a result container.
        Parameters:
        cmd - the commandline array to execute
        env - the environment variables, null if to inherit current ones
        cwd - the working directory for the command, null to ignore
        Returns:
        the results of the proces: exit code, stdout, stderr
        Throws:
        Exception - if something goes wrong
      • execute

        public static com.github.fracpete.processoutput4j.output.CollectingProcessOutput execute​(String[] cmd,
                                                                                                 HashMap<String,​String> env,
                                                                                                 String input,
                                                                                                 PlaceholderDirectory cwd)
                                                                                          throws Exception
        Executes the commandline array and returns a result container.
        Parameters:
        cmd - the commandline array to execute
        env - the environment variables, null if to inherit current ones
        input - the input to write to the process
        cwd - the working directory for the command, null to ignore
        Returns:
        the results of the proces: exit code, stdout, stderr
        Throws:
        Exception - if something goes wrong
      • execute

        public static com.github.fracpete.processoutput4j.output.CollectingProcessOutput execute​(String[] cmd,
                                                                                                 String[] env,
                                                                                                 PlaceholderDirectory cwd)
                                                                                          throws Exception
        Executes the commandline array and returns a result container.
        Parameters:
        cmd - the commandline array to execute
        env - the environment variables, null if to inherit current ones
        cwd - the working directory for the command, null to ignore
        Returns:
        the results of the proces: exit code, stdout, stderr
        Throws:
        Exception - if something goes wrong
      • execute

        public static com.github.fracpete.processoutput4j.output.CollectingProcessOutput execute​(String[] cmd,
                                                                                                 String[] env,
                                                                                                 String input,
                                                                                                 PlaceholderDirectory cwd)
                                                                                          throws Exception
        Executes the commandline array and returns a result container.
        Parameters:
        cmd - the commandline array to execute
        env - the environment variables, null if to inherit current ones
        input - the input to write to the process
        cwd - the working directory for the command, null to ignore
        Returns:
        the results of the proces: exit code, stdout, stderr
        Throws:
        Exception - if something goes wrong
      • execute

        public static com.github.fracpete.processoutput4j.output.CollectingProcessOutput execute​(String cmd,
                                                                                                 String[] env,
                                                                                                 String input,
                                                                                                 PlaceholderDirectory cwd)
                                                                                          throws Exception
        Executes the commandline array and returns a result container.
        Parameters:
        cmd - the commandline array to execute
        env - the environment variables, null if to inherit current ones
        input - the input to write to the process
        cwd - the working directory for the command, null to ignore
        Returns:
        the results of the proces: exit code, stdout, stderr
        Throws:
        Exception - if something goes wrong
      • toErrorOutput

        public static String toErrorOutput​(com.github.fracpete.processoutput4j.output.CollectingProcessOutput output)
        Returns an error output based on the information stored.
        Returns:
        the error output
      • getAvailableProcessors

        public static int getAvailableProcessors()
        Returns the number of available processors.
        Returns:
        the number of processors of the machine
      • getEnvironment

        public static HashMap<String,​String> getEnvironment()
        Returns the system's environment variables. Returns the same content as System.getenv(), but this hashmap can be modified.
        Returns:
        the environment variables as key-value pairs
      • getEnvironment

        public static HashMap<String,​String> getEnvironment​(BaseKeyValuePair[] envVars,
                                                                  boolean nullIfEmpty)
        Returns the system's environment variables with the provided ones overlayed on top.
        Parameters:
        envVars - the environment variables to overlay
        nullIfEmpty - if true and no custom environment variables, then returns null
        Returns:
        the environment variables as key-value pairs or null if nullIfEmpty is true and no custom environment variables
      • flattenEnvironment

        public static String[] flattenEnvironment​(HashMap<String,​String> env)
        Returns a flattened environment with key=value pairs.
        Parameters:
        env - the environment to flatten, can be null
        Returns:
        the flattened environment
      • convertHashMap

        protected static String[] convertHashMap​(HashMap<String,​String> env)
        Converts the environment variables stored in the hashmap to a string array ("key=value"). If the hashmap is null, null is returned as well.
        Parameters:
        env - the hashmap to convert
        Returns:
        the generated string array, null if hashmap was null
      • escapeBlanks

        public static String escapeBlanks​(String path)
        Escapes blanks in the path for Windows using the caret ("^"). Other platforms are not affected.n
        Parameters:
        path - the path to escape
        Returns:
        the escaped path