1

Hi I'm trying to execute shell commands from an app. Though the code runs with out any error, the output is not seen. As an example I'm trying to copy a file. This command works fine, while executing via shell command prompt. Here is the code I used..From the debug statements in LogCat, I could know all the code blocks are covered. But the file is not copied. Kindly suggest a solution. PS: I'm running this via emulator..

@Override
public void onClick(View v) {
    if (v.getId() == R.id.click_button){
        String[] commands = {"cp /mnt/sdcard/rr.txt /mnt/sdcard/zzzz.txt"};
        try {
            Log.d("Ac","before fun call.....");

            RunAsRoot(commands);
            Log.d("Ac","after fun call...");


        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}
 public void RunAsRoot(String[] cmds) throws IOException{
     Process p = Runtime.getRuntime().exec("su");
     DataOutputStream os = new DataOutputStream(p.getOutputStream());            
     for (String tmpCmd : cmds) {
             os.writeBytes(tmpCmd+"\n");
            Log.d("Ac","inside...");

     }           
     os.writeBytes("exit\n");  
    Log.d("Ac","outsidee...");

     os.flush();

}

}

2 Answers 2

1

Take a look on : RootTools
I am using this library in projects where I need to execute shell commands.

RootTools provides rooted developers a standardized set of tools for use in the development of rooted applications. In the end, we will accomplish this by providing developers with robust, easy-to-use libraries that will drastically improve development times as well as promote code reuse. This project is open to any proven developer that feels they have something to contribute. By pitching in together we can streamline our own processes, improve the effectiveness of our apps, learn new techniques, and provide a better experience for our users.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks EvZ.. I tried by replacing the method by RootTools functions. But getting exception java.util.concurrent.TimeoutException: write failed: EPIPE (Broken pipe)
Did you tried to run this command in adb shell?What was the result?
I ma not talking about the code,Did you tried to run your command in adb shell without the code?
1

The command "cp" is not available on Android shell, without installing BusyBox. Alternatively you could use:

cat /mnt/sdcard/rr.txt > /mnt/sdcard/zzzz.txt

as far as i know.

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.