I need to run some Bash commands in my Java app but all those scripts require sudo permission.
For example
p = Runtime.getRuntime().exec("sudo /opt/lampp/xampp start");
p.waitFor();
This doesn't work because I cannot write my sudo password, so I tried:
p = Runtime.getRuntime().exec("gksu /opt/lampp/xampp start");
p.waitFor();
And it works! But in some case I have 2 or more commands like (for example):
p = Runtime.getRuntime().exec("gksu /opt/lampp/xampp apachestart");
p2 = Runtime.getRuntime().exec("gksu /opt/lampp/xampp mysqlstart");
....
And user have to write sudo password 2/3 time. I created a bash file so I can execute this file with a single gksu. But the problem remains because user have to insert sudo password another time when I will recall this function.
At the end I tried:
String sudoPassword = askSudoPassword();
p = Runtime.getRuntime().exec("echo " + sudoPassword +
" && " + "/opt/lampp/xampp apachestart");
But it failed, I tried " | " instead " && " or "echo " + sudoPassword + " ; ....
I also tried to put password with a single quote into the string, but it failed again.
What can I do?