Vare Zon Vare Zon - 5 months ago 125x
Linux Question

How to execute bash command with sudo privileges in Java?

I'm using ProcessBuilder to execute bash commands:


public class Main {
public static void main(String[] args) {
try {
Process pb = new ProcessBuilder("gedit").start();
} catch (IOException e) {

But I want to make something like this:

Process pb = new ProcessBuilder("sudo", "gedit").start();

How to pass superuser password to bash?

("gksudo", "gedit")
will not do the trick, because it was deleted since Ubuntu 13.04 and I need to do this with available by default commands.


gksudo came back to Ubuntu 13.04 with the last update.


I think you can use this, but I'm a bit hesitant to post it. So I'll just say:

Use this at your own risk, not recommended, don't sue me, etc...

public static void main(String[] args) throws IOException {

    String[] cmd = {"/bin/bash","-c","echo password| sudo -S ls"};
    Process pb = Runtime.getRuntime().exec(cmd);

    String line;
    BufferedReader input = new BufferedReader(new InputStreamReader(pb.getInputStream()));
    while ((line = input.readLine()) != null) {