3

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

1

1 Answer 1

3

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")
Sign up to request clarification or add additional context in comments.

2 Comments

It works when I type in terminal and I can see the result. nativeProcess = Runtime.getRuntime().exec("su -c dhcpcd eth0"); InputStream is = nativeProcess.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); StringBuilder contents = new StringBuilder(); while ((line = br.readLine()) != null) { contents.append(line + "\n"); } I still get as null
@nneoneo- Actually the problem is when I execute that command the output I get is in the form of "fork to background, child pid type". DO you know anything about it, I was thinking it runs on background and connects to child process. SO to catch the response of the command "su -c dhcpcd eth0", there should be another way. If you know of it please do share your info

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.