Package oshi.util

Class ExecutingCommand


  • @ThreadSafe
    public final class ExecutingCommand
    extends java.lang.Object
    A class for executing on the command line and returning the result of execution.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.String[] DEFAULT_ENV  
      private static org.slf4j.Logger LOG  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private ExecutingCommand()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String getAnswerAt​(java.lang.String cmd2launch, int answerIdx)
      Return response on selected line index (0-based) after running selected command.
      private static java.lang.String[] getDefaultEnv()  
      static java.lang.String getFirstAnswer​(java.lang.String cmd2launch)
      Return first line of response for selected command.
      private static java.util.List<java.lang.String> getProcessOutput​(java.lang.Process p, java.lang.String[] cmd)  
      static java.util.List<java.lang.String> runNative​(java.lang.String cmdToRun)
      Executes a command on the native command line and returns the result.
      static java.util.List<java.lang.String> runNative​(java.lang.String[] cmdToRunWithArgs)
      Executes a command on the native command line and returns the result line by line.
      static java.util.List<java.lang.String> runNative​(java.lang.String[] cmdToRunWithArgs, java.lang.String[] envp)
      Executes a command on the native command line and returns the result line by line.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • LOG

        private static final org.slf4j.Logger LOG
      • DEFAULT_ENV

        private static final java.lang.String[] DEFAULT_ENV
    • Constructor Detail

      • ExecutingCommand

        private ExecutingCommand()
    • Method Detail

      • getDefaultEnv

        private static java.lang.String[] getDefaultEnv()
      • runNative

        public static java.util.List<java.lang.String> runNative​(java.lang.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 java.util.List<java.lang.String> runNative​(java.lang.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 java.util.List<java.lang.String> runNative​(java.lang.String[] cmdToRunWithArgs,
                                                                 java.lang.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
      • getProcessOutput

        private static java.util.List<java.lang.String> getProcessOutput​(java.lang.Process p,
                                                                         java.lang.String[] cmd)
      • getFirstAnswer

        public static java.lang.String getFirstAnswer​(java.lang.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 java.lang.String getAnswerAt​(java.lang.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