0

I'm currently creating an android application that will run some terminal commands. I am using a genymotion Nexus 5 (4.4.4) emulator that comes with root by default. However, to test some general commands I did the following:

Process process = Runtime.getRuntime().exec("su");
process = Runtime.getRuntime().exec(new String[]{"mkdir /sdcard/tmp1/"});
process = Runtime.getRuntime().exec(new String[]{"adb pull /sdcard/tmp3/new4.txt \"C:\\Users\\my_username\\Documents\\\""

When the "su" executes i get a message from an app named SuperUser saying I have have been given root privileges. However none of the other commands are executed. No tmp1 direcotry is made and I can't pull the new4.txt file, which I manually added to the emulator, to my documents folder.

Am I missing something in my path strings?

CIM
  • 41
  • 5
  • 1
    i can imagine each command runs on a different "session" so granted root access is not available for other commands, try to use `;` between commands, i think this way they will be executed with root permission?! ex `su ; mkdir /sdcard/tmp1/ ; ...` – Yazan Apr 20 '16 at 12:48
  • What happens if you run the same commands in an `adb shell`? – vbence Apr 20 '16 at 13:03
  • Thank you all... You help me find what I was looking for! – CIM Apr 26 '16 at 04:25

1 Answers1

0

When the "su" executes i get a message from an app named SuperUser saying I have have been given root privileges

So I guess you are running the code on the phone. In this case you are also running the third command (adb pull) on the phone, which you want to run on the host instead.

As this answer points out you are running consecutive commands in different sub-shells. So your first su will not matter in the second exec's context. To execute a command with su see the manual page of su. A simple exeample is:

su - -c "whoami"
Community
  • 1
  • 1
vbence
  • 20,084
  • 9
  • 69
  • 118
  • I forgot to mention I tried "su", "-c", "mkdir /sdcard/tmp1/" all three strings inside the exec() together but the tmp1 folder was never created. @vbence – CIM Apr 20 '16 at 12:33
  • 1
    Make sure `/sdcard` is actually mounted in `rw` mode. You can open an `adb shell` on the host and run the `mount` command to check the mounted filesystems (and their modes). – vbence Apr 20 '16 at 13:00
  • 1
    @Corn It would be also useful if you saw the output of the commands. Consult: http://stackoverflow.com/a/5711150/638471 – vbence Apr 20 '16 at 13:02