File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Change UNIX user in JAVA

 
Balaji Soundarajan
Ranch Hand
Posts: 59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3381
Mac MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>' ??
 
Balaji Soundarajan
Ranch Hand
Posts: 59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3381
Mac MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3381
Mac MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like this is rescue http://expectj.sourceforge.net/. Try it.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3381
Mac MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3381
Mac MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I execute this statement from Java code?
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20368
43
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're going to use Processes, search for and read the article "When Runtime.exec() won't" on JavaWorld.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3381
Mac MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11831
181
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've just posted an example of how to change the Unix user in the Unix forum. Take a look at the "setuid" topic.
 
Balaji Soundarajan
Ranch Hand
Posts: 59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic