Class ProcessUtils


  • public final class ProcessUtils
    extends Object
    Utility class for launching external process.
    Since:
    1.0
    Author:
    Laurent Jourdren
    • Method Detail

      • system

        public static int system​(String cmd)
                          throws IOException
        Execute a command.
        Parameters:
        cmd - command to execute
        Returns:
        the exit error of the program
        Throws:
        IOException - if an error occurs while executing the command
      • sh

        public static int sh​(List<String> cmd)
                      throws IOException
        Execute a command.
        Parameters:
        cmd - command to execute
        Returns:
        the exit error of the program
        Throws:
        IOException - if an error occurs while executing the command
      • sh

        public static int sh​(List<String> cmd,
                             File temporaryDirectory)
                      throws IOException
        Execute a command.
        Parameters:
        cmd - command to execute
        temporaryDirectory - temporary where create the temporary shell file
        Returns:
        the exit error of the program
        Throws:
        IOException - if an error occurs while executing the command
      • exec

        public static void exec​(String cmd,
                                boolean stdOutput)
                         throws IOException
        Execute a command with the OS.
        Parameters:
        cmd - Command to execute
        stdOutput - don't show the result of the command on the standard output
        Throws:
        IOException - if an error occurs while running the process
      • execWriteOutput

        public static void execWriteOutput​(String cmd,
                                           File outputFile)
                                    throws IOException
        Execute a command with the OS and save the output in file.
        Parameters:
        cmd - Command to execute
        outputFile - The output file
        Throws:
        IOException - if an error occurs while running the process
      • execToString

        public static String execToString​(String cmd)
                                   throws IOException
        Execute a command with the OS and return the output in a string.
        Parameters:
        cmd - Command to execute
        Returns:
        a string with the output the command
        Throws:
        IOException - if an error occurs while running the process
      • execToString

        public static String execToString​(String cmd,
                                          boolean addStdErr,
                                          boolean checkExitCode)
                                   throws IOException
        Execute a command with the OS and return the output in a string.
        Parameters:
        cmd - Command to execute
        addStdErr - add the output of stderr in the result
        Returns:
        a string with the output the command
        Throws:
        IOException - if an error occurs while running the process
      • logEndTime

        public static void logEndTime​(Process p,
                                      String cmd,
                                      long startTime)
                               throws IOException
        Log the time of execution of a process.
        Parameters:
        p - Process to log
        cmd - Command of the process
        startTime - Start time in ms
        Throws:
        IOException - if an error occurs at the end of the process
      • throwExitCodeException

        public static void throwExitCodeException​(int exitCode,
                                                  String command)
                                           throws IOException
        Throw an IOException if the exit code of a process is not equals to 0.
        Parameters:
        exitCode - the exit code
        command - the executed command
        Throws:
        IOException - if the exit code if not 0
      • getExecutablePids

        public static Set<Integer> getExecutablePids​(String executableName)
        Return a set withs pid of existing executable.
        Returns:
        a set of integers with pid of existing executable
      • waitUntilExecutableRunning

        public static void waitUntilExecutableRunning​(String executableName)
        Wait the end of the execution of all the instance of an executable.
        Parameters:
        executableName - name of the executable
      • waitRandom

        public static void waitRandom​(int maxMilliseconds)
        Wait a random number of milliseconds.
        Parameters:
        maxMilliseconds - the maximum number of milliseconds to wait
      • execThreadOutput

        public static void execThreadOutput​(String cmd)
                                     throws IOException
        Execute a command and write the content of the standard output and error to System.out and System.err.
        Parameters:
        cmd - Command to execute
        Throws:
        IOException - if an error occurs while executing the command
      • execThreadOutput

        public static void execThreadOutput​(String[] cmd)
                                     throws IOException
        Execute a command and write the content of the standard output and error to System.out and System.err.
        Parameters:
        cmd - array with the command to execute
        Throws:
        IOException - if an error occurs while executing the command