3

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.

Kiril Kirilov
  • 11,167
  • 5
  • 49
  • 74
  • Do these commands work when run directly from terminal? – Tomasz Nurkiewicz Apr 04 '12 at 12:16
  • First command prints exactly the same if I'm not root(if I'm root, it unmounts the folder successfully). Second command prompts for password. – Kiril Kirilov Apr 04 '12 at 12:20
  • you could allow the user running your java program to unmount without entering a password. Take a look at the file /etc/sudoers (editable with command "visudo") for a few examples. – Dirk Trilsbeek Apr 04 '12 at 12:25
  • possible duplicate of [How to input password to sudo using java Runtime?](http://stackoverflow.com/questions/9716609/how-to-input-password-to-sudo-using-java-runtime) – Tomasz Nurkiewicz Apr 04 '12 at 12:26
  • This answer may be useful http://stackoverflow.com/questions/9716609/how-to-input-password-to-sudo-using-java-runtime – mguymon Apr 04 '12 at 12:27
  • To edit /etc/sudoers I must previously entered with the root account. But since I only can do it manually, it is not very useful. I need root privileges in my java process. – Kiril Kirilov Apr 04 '12 at 12:30

1 Answers1

3

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");
    
Joni
  • 108,737
  • 14
  • 143
  • 193