Class RuntimeUtil

java.lang.Object
org.javalite.common.RuntimeUtil

public class RuntimeUtil extends Object
Utility class to shell out system commands. Use for quick execution of external processes that will not generate a lot of output.
Author:
igor on 1/20/17.
  • Constructor Details

    • RuntimeUtil

      public RuntimeUtil()
  • Method Details

    • execute

      public static RuntimeUtil.Response execute(int maxBuffer, String... command)
      Executes an external command and provides results of execution at the current location. Will accumulate limited output from the external process.
      Parameters:
      command - array containing the command to call and its arguments.
      maxBuffer - max size of buffers out, err. An external process may produce a lot of output, be careful setting to a large value. The buffer will not be allocated to this size at the start, but will grow until it reaches it. The program will continue toi execute, and the buffer will be 'tailing' the output of the external process.
      Returns:
      instance of RuntimeUtil.Response with result of execution.
    • execute

      public static RuntimeUtil.Response execute(int maxBuffer, File dir, List<String> envVars, String... command)
      Executes an external command and provides results of execution. Will accumulate limited output from the external process.
      Parameters:
      command - array containing the command to call and its arguments.
      maxBuffer - max size of buffers out, err. An external process may produce a lot of output, be careful setting to a large value. The buffer will not be allocated to this this size at the start, but will grow until it reaches it. The program will continue toi execute, and the buffer will be 'tailing' the output of the external process.
      dir - - location of process execution. Pass null to execute at current location of the calling process.
      envVars - a list of environment variables to pass to the process. The format for each string in a list: "name=value".
      Returns:
      instance of RuntimeUtil.Response with result of execution.
    • execute

      public static RuntimeUtil.Response execute(int maxBuffer, File dir, String... command)
      Executes an external command and provides results of execution. Will accumulate limited output from the external process.
      Parameters:
      command - array containing the command to call and its arguments.
      maxBuffer - max size of buffers out, err. An external process may produce a lot of output, be careful setting to a large value. The buffer will not be allocated to this this size at the start, but will grow until it reaches it. The program will continue toi execute, and the buffer will be 'tailing' the output of the external process.
      dir - - location of process execution. Pass null to execute at current location of the calling process.
      Returns:
      instance of RuntimeUtil.Response with result of execution.
    • execute

      public static RuntimeUtil.Response execute(String... command)
      Executes an external command and provides results of execution. Will accumulate limited output from the external process. Defaults to max 2048 characters in each buffer: out, err and only shows the tail of each buffer.
      Parameters:
      command - array containing the command to call and its arguments.
      Returns:
      instance of RuntimeUtil.Response with result of execution.
    • execute

      public static RuntimeUtil.Response execute(String command)
      Convenience method, does the same as execute(String...), but will automatically convert a full command string to tokens for convenience. Here is how to call:
       System.out.println(execute("ls -ls").out);
       
      Parameters:
      command - - a single string representing a command and its arguments.
      Returns:
      instance of RuntimeUtil.Response with result of execution.