3

I need to run shell commands (in my case ipfw) in Java with root privileges. I use ProcessBuilder to run commands with no privileges:

Process p = new ProcessBuilder("ls","/").start();

BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));

String s;
while((s=br.readLine()) != null) {
    System.out.println(s);
}

This code work fine but I need to run something like sudo root command. How can I do?

I need to run this program in Eclipse, Mac OS X 10.6.8

Edit: for now I use sudo java nameclass from terminal but in this way I have to manually add all the referenced classes.

1 Answer 1

6

Easy!

  1. Set up your sudoers to not require a password for ipfw:

    user = NOPASSWD: /usr/sbin/ipfw
    
  2. Use sudo as usual.

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

4 Comments

sudo visudo. Edit to your heart's content.
Do I must add this line under # User privilege specification?
Yes, you should add it there.
I've put the line there but it response me with error in that line. I've also substituted user with myusername but the error is the same.

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.