Skip to content

Instantly share code, notes, and snippets.

@androidlover5842
Created November 5, 2016 10:58
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 androidlover5842/2594b23923fe7a5063d97c45567c09ed to your computer and use it in GitHub Desktop.
Save androidlover5842/2594b23923fe7a5063d97c45567c09ed to your computer and use it in GitHub Desktop.
Run as root without Shell.SU.run
public String runAsRoot() {
try {
// Executes the command.
Process process = Runtime.getRuntime().exec(command);
// Reads stdout.
// NOTE: You can write to stdin of the command using
// process.getOutputStream().
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
int read;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();
while ((read = reader.read(buffer)) > 0) {
output.append(buffer, 0, read);
}
reader.close();
// Waits for the command to finish.
process.waitFor();
return output.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment