wood burning stoves 2.0*
The moose likes Java in General and the fly likes Change UNIX user in JAVA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Change UNIX user in JAVA" Watch "Change UNIX user in JAVA" New topic
Author

Change UNIX user in JAVA

Balaji Soundarajan
Ranch Hand

Joined: Jan 23, 2009
Posts: 59
I have wriiten java class to excute on Unix machine.The UNIX user ID is passed through command line arguments to this java class and have to perform all the work as that user which passed thru command line arguments.How to change the UNIX user using user ID in JAVA ?

Please give me any idea to implement this.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3350

Balaji Soundarajan wrote:How to change the UNIX user using user ID in JAVA ?

Please give me any idea to implement this.


What do you mean by 'change the unix user id'? Do you mean to say 'Run As <thisUser>' ??


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Balaji Soundarajan
Ranch Hand

Joined: Jan 23, 2009
Posts: 59
yes,I have to perform all the operations on UNIX in java as the user which is passed thru command line argument.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3350

Thats a good aspect. Thanks. I have never come across.

Have been googling around by the time I posted my first reply here .

I have NOT got any satisfied and straight forward results so far.

But to give you a work around, you can first issue a native command of switching user ("su <userEnteredInCommandLine>") via the external process you launched. This would effectively change the user for running the program. Subsequently you can run the java program you wish to run.


Pseudo code :



Does that help?

But I just doubt would there be a continuity between these two attempts!.

Meanwhile, If I get another solution/work around shall post it here. Good luck
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3350

Looks like this is rescue http://expectj.sourceforge.net/. Try it.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

That's not the same thing.

Once you su you're that user until you exit the su shell.

sudo will run a given command on the command line and you'll be the original user when the command has completed.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3350

David Newton wrote:That's not the same thing.

Once you su you're that user until you exit the su shell.

sudo will run a given command on the command line and you'll be the original user when the command has completed.


Thats great David. Thanks for the clarification.

However, we are NOT really using the shell here. Right? It is through an external process he is using via/through Java. Hence i doubted on the effect of 'su' for the subsequent commands. Hope I made it clear.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3350

Hi Balaji,

Try using this



This -c switch facilitates you to specify a process to execute as a specified user in the 'su' command. It would probably work and give you a relief
Balaji Soundarajan
Ranch Hand

Joined: Jan 23, 2009
Posts: 59
How can I execute this statement from Java code?
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3350

You have to launch it through java.lang.Process instance.

Google it for launching an external/native process via Java. You can get it. If you face any difficulty, please post back. We can help you out.
Balaji Soundarajan
Ranch Hand

Joined: Jan 23, 2009
Posts: 59
I tried like p=Runtime.getRuntime().exec("su root") and its not executed.its working for all other commands like ls -a
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

If you're going to use Processes, search for and read the article "When Runtime.exec() won't" on JavaWorld.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3350

Balaji Soundarajan wrote:I tried like p=Runtime.getRuntime().exec("su root") and its not executed.its working for all other commands like ls -a


Did you check with "su root -c java YourJavaPgm"? Shall try with the same in my machine and get back!
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3350

Rob Prime wrote:If you're going to use Processes, search for and read the article "When Runtime.exec() won't" on JavaWorld.


Yes, thats correct Rob . There are some pitfalls to it.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11490
    
  95

I've just posted an example of how to change the Unix user in the Unix forum. Take a look at the "setuid" topic.


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Balaji Soundarajan
Ranch Hand

Joined: Jan 23, 2009
Posts: 59
Thats good Andrew.I got some idea on that.

I will get user id in the java code form command line argument like

java StorageServer Start -U 100

here 100 is user id and i will get this user id in the java code and have to make him as a owner for all the operations thru JAVA.

Plaese tell me how to achieve this?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Change UNIX user in JAVA