Class ExecutingCommand
java.lang.Object
oshi.util.ExecutingCommand
A class for executing on the command line and returning the result of execution.
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringgetAnswerAt(String cmd2launch, int answerIdx) Return response on selected line index (0-based) after running selected command.static StringgetFirstAnswer(String cmd2launch) Return first line of response for selected command.static StringgetFirstPrivilegedAnswer(String cmd2launch) Return first line of response for selected privileged command.Executes a command on the native command line and returns the result.Executes a command on the native command line and returns the result line by line.Executes a command on the native command line and returns the result line by line.runPrivilegedNative(String cmdToRun) Executes a command that may require elevated privileges.
-
Method Details
-
runNative
Executes a command on the native command line and returns the result. This is a convenience method to callRuntime.exec(String)and capture the resulting output in a list of Strings. On Windows, built-in commands not associated with an executable program may requirecmd.exe /cto 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
Executes a command on the native command line and returns the result line by line. This is a convenience method to callRuntime.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 stringscmd.exeand/cto 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
Executes a command on the native command line and returns the result line by line. This is a convenience method to callRuntime.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 stringscmd.exeand/cto be prepended to the array.- Parameters:
cmdToRunWithArgs- Command to run and args, in an arrayenvp- 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
-
getAnswerAt
Return response on selected line index (0-based) after running selected command.- Parameters:
cmd2launch- String command to be launchedanswerIdx- 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
Executes a command that may require elevated privileges. If already running as root or no sudo prefix is configured, delegates directly torunNative(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
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
-