6

I am getting an error while running this code and the error is " Root access rejected [java.io.IOException] : Error running exec(). Command: [su] Working Directory: null Environment: null"

try 
{
    suProcess = Runtime.getRuntime().exec("su");
    DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
    DataInputStream osRes = new DataInputStream(suProcess.getInputStream());

    if (null != os && null != osRes) 
    {
        // Getting the id of the current user to check if this is root
        os.writeBytes("id\n");
        os.flush();
        String currUid = osRes.readLine();
        boolean exitSu = false;
        if (null == currUid) 
        {
            retval = false;
            exitSu = false;
            Log.d("ROOT", "Can't get root access or denied by user");
        }
        else if (true == currUid.contains("uid=0"))
        {
        retval = true;
        exitSu = true;
        Log.d("ROOT", "Root access granted");
        } 
        else 
        {
        retval = false;
        exitSu = true;
        Log.d("ROOT", "Root access rejected: " + currUid);
        }

        if (exitSu)
        {
           os.writeBytes("exit\n");
           os.flush();
        }
    }
} 
catch (Exception e)
{
    Log.d("ROOT", "Root access rejected [" + e.getClass().getName()
                + "] : " + e.getMessage());
}
Gwenc37
  • 2,064
  • 7
  • 18
  • 22
bhangu_23
  • 61
  • 1
  • 5

3 Answers3

2

This is because apps do not have a super user permission in the android system.

Shivam Verma
  • 7,973
  • 3
  • 26
  • 34
  • I am trying to implement a code in which i need to compress a video using android but unable to find any relevant API to do that.Could you guide me please? – bhangu_23 Jun 18 '14 at 08:02
2

Well, first of all ... you need to get your device rooted. A little Google Search will help with that.

If your application will be running on anonymous client devices, then you should better check first if the device is rooted or not. My library has a root availability check method which looks like below, but it doesn't work in a few cases.

    public static boolean hasRoot() {
        String tags = Build.TAGS;
        if ((tags != null) && tags.contains("test-keys"))
            return true;
        try {
            if (new File("/system/app/Superuser.apk").exists())
                return true;
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        String[] commands = {
            "/system/xbin/which su",
            "/system/bin/which su",
            "which su"
        };
        for (String command : commands) {
            try {
                Runtime.getRuntime().exec(command);
                return true;
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }
        return false;
    }

Then you can proceed with your procedure to execute a superuser shell & check for uid.

Or if you want to simplify things, you can have a look at my open-source Android CLI library with which, this task would be simpler. You can find the github project here.

Michael Kohne
  • 11,888
  • 3
  • 47
  • 79
VPZ
  • 741
  • 8
  • 19
  • When using this code, it shows "java.io.IOException: Error running exec(). Command: [/system/xbin/which, su] Working Directory: null Environment: null" – bhangu_23 Jun 18 '14 at 09:37
  • 1
    Most probably you aren't rooted then. Go for rooting first then proceed. – VPZ Jun 18 '14 at 15:24
1

If your phone has root access then you can fire shell scripts using Process and Runtime class..

see the below link:-

Run a shell command as root on android?

http://en.wikipedia.org/wiki/Rooting_%28Android_OS%29

Community
  • 1
  • 1
duggu
  • 37,851
  • 12
  • 116
  • 113
  • I am trying to implement a code in which i need to compress a video using android but unable to find any relevant API to do that.Could you guide me please? – bhangu_23 Jun 18 '14 at 08:01