Some choices for running a command as the 'root' user are :
1) Switch to 'root' before executing the command. This will work on all Linux distributions but of course you have to be able to switch to 'root'.
2) Use 'sudo' with the '-S' switch and pass the user's password to the Process stdin. This requires the user to be registered in the sudoers configuration file (the prime user is by default on Debian based distributions) with permissions set to execute the command being run. This either requires one to put the the password in the program (very very insecure) OR to create a dialog in which the user enters his password OR to pass the password as an argument to the Java program.
3) Use 'gksudo' which automatically brings up a password dialog . This is my preferred approach and the one I always use if I can. It also requires the user to be registered in the sudoers configuration file and, of course, that 'gksudo' is available.
For this 3rd approach your command would be
Note - you should really handle the process stderr stream and the process exit code which is explained in http://www.javaworld.com/jw-12-2000/jw-1229-traps.html . You should read ALL the sections and implement ALL the recommendations.
Note 1 - you might do better to use ProcessBuilder. It has, in my view, a better interface and is what Runtime.exec() uses behind the scenes. You still need to read the 'traps' article since it is still applicable and the recommendations still apply..
dharma cool wrote:i am trying to execute the linux super user commands via java program.
The first question I have is: why?
This is NOT what Java was designed for; and I, for one, would be VERY suspicious of any such program running on one of my systems (15 years as a Unix/Linux admin), and if I discovered one, I would probably remove it immediately and silently.
Perhaps if you explained what you're trying to achieve, we could suggest a better (and safer) alternative.
Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
dharma lingam s
Joined: Aug 29, 2013
i installed some open source tools that things are work only on super user mode. to make easier my job i am doing automation so that i am asking..
@Winston Gutkowski if you know way means suggest me.
dharma lingam s wrote:i installed some open source tools that things are work only on super user mode.
Well, that right there sounds alarm bells for me. What are these "tools" for, and why do they need superuser access?
to make easier my job i am doing automation so that i am asking..
I need a bit of background before I start advising anything.
1. Is this for use on your home computer?
2. If for work:
(a) Have you informed your admins that you've installed these products?
(b) Have you informed them of your intention to automate this process, and got their approval?
Anything requiring superuser access is inherently unsafe, so you'd better be absolutely sure that you trust these "tools" before you even consider such a move. At most places I've worked, trying anything like this without written management approval would be grounds for dismissal; so think very carefully before you just plough ahead.
Furthermore "sudo" is mainly a command-line function, and it's designed to be used on individual commands, not via something like Java (which may well be why you're having so much trouble). I suspect you'd be much better off trying to get your java program running with SU privileges, and then invoke your tool but, to be honest, the whole approach sounds a bit clunky to me.
Is this not, for example, something that you could "automate" through cron?
dharma lingam s
Joined: Aug 29, 2013
Winston Gutkowski : xen hypervisor. And this is my system only. so no problem for calling the sudo by JAVA program.