Created
November 5, 2016 10:58
-
-
Save androidlover5842/2594b23923fe7a5063d97c45567c09ed to your computer and use it in GitHub Desktop.
Run as root without Shell.SU.run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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