admin管理员组

文章数量:1399997

I am trying to run a command as root on a rooted device. I am using the command su root 'whoami' to check if it works. This is my code:

public String runAsRoot() {

        try {
            Process process = Runtime.getRuntime().exec("su root 'whoami'");

            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();

            process.waitFor();

            return output.toString();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

The function should output root if it works. When trying the command using adb (i.e., adb shell root 'whoami') it return root as expected. But when running it from the app it crashes and I get this error:

FATAL EXCEPTION: main (Ask Gemini)
Process: me.rapierxbox.test, PID: 9207
java.lang.RuntimeException: java.io.IOException: Cannot run program "su": error=13, Permission denied
at me.rapierxbox.test.MainActivity.runAsRoot(MainActivity.java:148)
at me.rapierxbox.test.MainActivity.lambda$onCreate$1$me-rapierxbox-test-MainActivity(MainActivity.java:50)
at me.rapierxbox.test.MainActivity$$ExternalSyntheticLambda3.onClick(D8$$SyntheticClass:0)
at android.view.View.performClick(View.java:5610)
Caused by: java.io.IOException: Cannot run program "su": error=13, Permission denied
at java.lang.ProcessBuilder.start(ProcessBuilder.java:983)
at java.lang.Runtime.exec(Runtime.java:691)
at java.lang.Runtime.exec(Runtime.java:524)
at java.lang.Runtime.exec(Runtime.java:421)
at me.rapierxbox.test.MainActivity.runAsRoot(MainActivity.java:131)
                                                                                                        

Using the world-executable /system/xbin/su also doesnt work with the same error.

本文标签: