1

I'm working on a cross-platform installation utility written in Java. I would like to be able to execute shell commands, for example "apt-get ..." on Linux. I'm using the ProcessBuilder API for this purpose.

Unfortunately, some commands require root privileges. I would like to execute the following shell command as root:

bash -c apt-get install -y [some package]

If I try to add "sudo" before "apt-get" nothing works because there is no terminal "sudo" can get the password from.

What should I do in order to ask the user for a password and then execute the above mentioned shell command?

A short code snippet would be very appreciated! Thanks in advance! Max

2
  • 1
    Run the installer with root privileges is that is what is required. Commented Aug 2, 2013 at 15:21
  • I'm planning to use this installer from within Java Web Start. It's not a good idea to gain JavaWS too much privileges. Moreover, I would like to execute root commands only if required. My installer checks first if any installation is necessary or not... Commented Aug 2, 2013 at 15:32

2 Answers 2

3

You could run gksudo <your command> but this ties your application to a certain UI, e.g. gnome, kde, etc.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! It works now. Binding to UI is not an issue to me because my project is targeting primarily graphical environments.
@user1815381: We usually accept answers if they help. I hope you do the same, it is has helped. Thanks!
Sorry! I didn't know that. Thank you for the hint!
0

You could write all the the root-tasks to a scriptfile and ask the user to execute it with root privileges. I know this sounds clumsy, but its done that way by quite a lot of "enterprise level" software vendors (e.g. the (Java-) installer of the Oracle RDBMS at least up to 11g).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.