I have a requirement where I need to run a script in ubuntu with sudo on click of a button in swing UI.
In mac I just use the open command with Runtime.getRuntime.exec("open appname.app")
and it launches a terminal asking me for the password and executes the script but in linux it doesnt work that way.
I've tried the solution in How to execute bash command with sudo privileges in Java? but didn't work for me.
public static void main(String[] args) throws IOException {
String[] cmd = {"/bin/bash","-c","echo mysudopassword| sudo -S <path of my script>"};
Process pb = Runtime.getRuntime().exec(cmd);
String line;
BufferedReader input = new BufferedReader(new InputStreamReader(pb.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
}
input.close();
}
Even tried it this way as below
{"/bin/bash","-c","echo \"password\"| sudo -S ls"};
Is it possible to pass the password to sudo and then launch the script or open a terminal asking for the password and then launch it ?
I'm using java8 with ubuntu 18.04