user1810931
user1810931

Reputation: 658

executing shell commands programmatically in Android

I have two commands to execute and get back the data My .sh file as two commands and it looks like this

su dhcpcd eth0

when I try to execute the .sh command in my Android terminal by typing as sh filename.sh It does not give me output but when I do execute it by typing individual line it works. SO when I program as

nativeProcess = Runtime.getRuntime().exec("su");
nativeProcess = Runtime.getRuntime().exec("dhcpcd eth0");
while ((line = br.readLine()) != null) 
{
    contents.append(line + "\n");
}

What is wrong in this? I get the output contents as null

Upvotes: 3

Views: 6500

Answers (1)

nneonneo
nneonneo

Reputation: 179412

exec in Java starts a new process. So the first line makes a new su process, which is going to simply sit there and wait for your input. The second line starts a new dhcpcd process, which won't be privileged and so won't produce useful output.

What you want is to run dhcpcd using su, typically like this:

exec("su -c dhcpcd eth0")

Upvotes: 3

Related Questions