Kiril Kirilov
Kiril Kirilov

Reputation: 11257

How to unmount a Linux folder from Java

I tried:

final ProcessBuilder pb = new ProcessBuilder("umount", "foldername");
final Process p = pb.start();

Throws

umount: /home/user/foldername is not in the fstab (and you are not root)

I tried

final ProcessBuilder pb = new ProcessBuilder("sudo","umount", "foldername");
final Process p = pb.start();

Throws

sudo: sorry, you must have a tty to run sudo

I got the root password, but can't provide it to the ProcessBuilder. Also I cannot edit fstab (or whatever is needed to be edited), because it is remote virutal machine started on a remote server from saved OS image.

I just want to run the command as root.

Upvotes: 3

Views: 1129

Answers (1)

Joni
Joni

Reputation: 111279

You have a couple of options:

  1. Make the controlling terminal available for sudo so that the user can type the password there.

    pb = new ProcessBuilder("sh", "-c", "sudo umount foldername </dev/tty");
    Process p = pb.start();
    p.waitFor();
    
  2. Execute the program with gksudo rather than sudo. Systems that use GTK+ often come with the gksu package as a graphical interface for su and sudo.

    pb = new ProcessBuilder("gksudo","umount", "foldername");
    
  3. Open a terminal emulator window for sudo:

    pb = new ProcessBuilder("xterm","-e","sudo","umount","foldername");
    

Upvotes: 3

Related Questions