Search code examples
javaterminalrootsudomount

Executing mount command in Java on Android


I'm trying to write Java code that executes some terminal commands. The code should execute this command sudo mount -o loop system.img system. But there are several problems. First, to execute this command I have to be root. I know that I can be by sudo su, but how can I stay as root when I close the terminal window? If I use the command sudo mount -o loop system.img system how can I provide the password in the Java code?

The second issue is: can I execute the command as below?

File f2 = new File("/home/user1/Desktop/aDirectory");
String[] commands = new String[]{"sudo mount", "-o", "loop", "/home/user1/Desktop/aDirectory/system.img"};

Runtime.getRuntime().exec(commands, null, f2);  

I think I can't. So how can I do it? Any ideas?

Notes: system.img is a compiled Android os file. and the system is an empty directory. The thing I'm trying to do is mount the system.img file into the system directory.


Solution

  • The problem was solved, by using shell script.

    I wrote a script includes just this line :

    echo myPassword | sudo -S mount -o loop system.img system
    

    then I run it in my java code, such :

    Runtime.getRuntime().exec("sh 1.sh");