Class ExecutingCommand

java.lang.Object
oshi.util.ExecutingCommand

@ThreadSafe public final class ExecutingCommand extends Object
A class for executing on the command line and returning the result of execution.
  • Method Details

    • runNative

      public static List<String> runNative(String cmdToRun)
      Executes a command on the native command line and returns the result. This is a convenience method to call Runtime.exec(String) and capture the resulting output in a list of Strings. On Windows, built-in commands not associated with an executable program may require cmd.exe /c to be prepended to the command.
      Parameters:
      cmdToRun - Command to run
      Returns:
      A list of Strings representing the result of the command, or empty string if the command failed
    • runNative

      public static List<String> runNative(String[] cmdToRunWithArgs)
      Executes a command on the native command line and returns the result line by line. This is a convenience method to call Runtime.exec(String[]) and capture the resulting output in a list of Strings. On Windows, built-in commands not associated with an executable program may require the strings cmd.exe and /c to be prepended to the array.
      Parameters:
      cmdToRunWithArgs - Command to run and args, in an array
      Returns:
      A list of Strings representing the result of the command, or empty string if the command failed
    • runNative

      public static List<String> runNative(String[] cmdToRunWithArgs, String[] envp)
      Executes a command on the native command line and returns the result line by line. This is a convenience method to call Runtime.exec(String[]) and capture the resulting output in a list of Strings. On Windows, built-in commands not associated with an executable program may require the strings cmd.exe and /c to be prepended to the array.
      Parameters:
      cmdToRunWithArgs - Command to run and args, in an array
      envp - array of strings, each element of which has environment variable settings in the format name=value, or null if the subprocess should inherit the environment of the current process.
      Returns:
      A list of Strings representing the result of the command, or empty string if the command failed
    • getFirstAnswer

      public static String getFirstAnswer(String cmd2launch)
      Return first line of response for selected command.
      Parameters:
      cmd2launch - String command to be launched
      Returns:
      String or empty string if command failed
    • getAnswerAt

      public static String getAnswerAt(String cmd2launch, int answerIdx)
      Return response on selected line index (0-based) after running selected command.
      Parameters:
      cmd2launch - String command to be launched
      answerIdx - int index of line in response of the command
      Returns:
      String whole line in response or empty string if invalid index or running of command fails
    • runPrivilegedNative

      public static List<String> runPrivilegedNative(String cmdToRun)
      Executes a command that may require elevated privileges. If already running as root or no sudo prefix is configured, delegates directly to runNative(String). If a sudo prefix is configured and the command is in the allowlist, prepends the prefix before execution. If the command is not in the allowlist, attempts to run without the prefix (the command may still succeed if permissions allow).
      Parameters:
      cmdToRun - Command to run
      Returns:
      A list of Strings representing the result of the command, or empty list if the command failed
    • getFirstPrivilegedAnswer

      public static String getFirstPrivilegedAnswer(String cmd2launch)
      Return first line of response for selected privileged command.
      Parameters:
      cmd2launch - String command to be launched with privilege escalation if needed
      Returns:
      String or empty string if command failed or privilege requirements were not met