Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6337019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:10:16+00:00 2026-05-24T19:10:16+00:00

I’m developing my first Android application, and I’m curious if there are any standard

  • 0

I’m developing my first Android application, and I’m curious if there are any “standard” ways for executing privileged shell commands. I’ve only been able to find one way to do it, by executing su, and then appending my commands to stdin of the su process.

DataOutputStream pOut = new DataOutputStream(p.getOutputStream());
DataInputStream pIn = new DataInputStream(p.getInputStream());

String rv = "";

// su must exit before its output can be read
pOut.writeBytes(cmd + "\nexit\n");
pOut.flush();

p.waitFor();

while (pIn.available() > 0)
    rv += pIn.readLine() + "\n";

I’ve read about wrapping privileged (superuser) calls up in JNI: is this possible? If so, how would one go about accomplishing it? Other than that, are there any other ways of calling privileged instructions from Java?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T19:10:17+00:00Added an answer on May 24, 2026 at 7:10 pm

    As far as I know, you can only run command-line commands using root privileges. You can use this generic class I made that wraps the root access in your code:
    http://muzikant-android.blogspot.com/2011/02/how-to-get-root-access-and-execute.html

    All you need to do is extend this class and override the getCommandsToExecute method to return the commands you want to execute as root.

    public abstract class ExecuteAsRootBase
    {
       public static boolean canRunRootCommands()
       {
          boolean retval = false;
          Process suProcess;
    
          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)
          {
             // Can't get root !
             // Probably broken pipe exception on trying to write to output stream (os) after su failed, meaning that the device is not rooted
    
             retval = false;
             Log.d("ROOT", "Root access rejected [" + e.getClass().getName() + "] : " + e.getMessage());
          }
    
          return retval;
       }
    
       public final boolean execute()
       {
          boolean retval = false;
    
          try
          {
             ArrayList<String> commands = getCommandsToExecute();
             if (null != commands && commands.size() > 0)
             {
                Process suProcess = Runtime.getRuntime().exec("su");
    
                DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
    
                // Execute commands that require root access
                for (String currCommand : commands)
                {
                   os.writeBytes(currCommand + "\n");
                   os.flush();
                }
    
                os.writeBytes("exit\n");
                os.flush();
    
                try
                {
                   int suProcessRetval = suProcess.waitFor();
                   if (255 != suProcessRetval)
                   {
                      // Root access granted
                      retval = true;
                   }
                   else
                   {
                      // Root access denied
                      retval = false;
                   }
                }
                catch (Exception ex)
                {
                   Log.e("ROOT", "Error executing root action", ex);
                }
             }
          }
          catch (IOException ex)
          {
             Log.w("ROOT", "Can't get root access", ex);
          }
          catch (SecurityException ex)
          {
             Log.w("ROOT", "Can't get root access", ex);
          }
          catch (Exception ex)
          {
             Log.w("ROOT", "Error executing internal operation", ex);
          }
    
          return retval;
       }
       protected abstract ArrayList<String> getCommandsToExecute();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm making a simple page using Google Maps API 3. My first. One marker
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.