Skip to content

Instantly share code, notes, and snippets.

@cooliscool
Created December 27, 2023 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cooliscool/b4741292c17b859334642f586c6d6b59 to your computer and use it in GitHub Desktop.
Save cooliscool/b4741292c17b859334642f586c6d6b59 to your computer and use it in GitHub Desktop.
Android Shell Command executor class.
package com.moo.myapplication2;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CommandExecutor {
public static String executeCommand(String command) {
StringBuilder output = new StringBuilder();
try {
// Execute the command
Process process = Runtime.getRuntime().exec(command);
// Read the output of the command
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
output.append(line).append("\n");
}
BufferedReader stderr = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String errLine;
while ((errLine = stderr.readLine()) != null) {
output.append(errLine).append("\n");
}
// Wait for the command to complete
int exitCode = process.waitFor();
// Log the exit code
output.append("\nExit code: ").append(exitCode);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
output.append("Error executing command: ").append(e.getMessage());
}
return output.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment