0

I have a sh file which conatins a many sudo commands, say a script as below:

test.sh

#!/bin/sh
sudo apt-get install gedit
sudo date --set="2014/02/20 10:00"

When I execute the test.sh from terminal it works fine. Problem is when I try to execute this file from a java class. The script doesn't execute. I tried this:

Process proc = Runtime.getRuntime().exec("/bin/sh","/home/priyatam/test.sh");

Does anyone have an idea to execute this sudo command from java class? Please share.

1 Answer 1

1

well as the script will be outputting the prompt to the screen, it will be necessary to capture the processes inputstream

InputStream stdout = process.getInputStream ();

I would also recommend using

ProcessBuilder builder = new ProcessBuilder("/home/user/test.sh");
builder.redirectErrorStream(true);
Process process = builder.start();
Sign up to request clarification or add additional context in comments.

2 Comments

I am not getting any prompt even
to be honest I would start off with a shell, not the script. Have a look at this link singztechmusings.wordpress.com/2011/06/21/…

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.