Changing running permission to Root

lirong
lirong used Ask the Experts™
on
Hi

I want to write a simpe Java application that has a Root permission (like modifying the /etc/hosts file).

I want to run this application with a X user that that user will have abilities to modify the /etc/hosts file.

Is it possible to do that in Java ?

I know that in C,C++ I can do that.

Thanks,

Liron
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Wrap it into a script runroot.sh that is owned by root.
chgrp ALLOWEDUSER runroot.sh;
chown root ALLOWEDUSER
chmod g+rx,u+s runroot.sh

The last command needs to be issued by root.
sudo your-java-app

Author

Commented:
I didn't followed you.
Let's say that I have a application called: My.class
This application needs to execute the command "mv /etc/hosts /etc/hosts.sav"

I'm running this application under user: lirong.

In order to execute the move, I need to execute it in a root permission.
How do I do that ?
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

either run the hole application as root (not recommended):
  sudo java My.class
or call Java's embeded system call:
    System.exec("su root -c "mv /etc/hosts /etc/hosts.sav");

(not shure about the method name: System.exec() )

Author

Commented:
The above isn't correct.
Please help !!!
what is wrong?
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Recommendation: Split between csar and ahoffman

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

DominicCronin
EE Cleanup Volunteer
current update:
  java.lang.Runtime.exec("su root -c "mv /etc/hosts /etc/hosts.sav")

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial