This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Linux / UNIX and the fly likes check UID with Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "check UID with Java" Watch "check UID with Java" New topic
Author

check UID with Java

Nikolay Tsonkov
Greenhorn

Joined: May 31, 2008
Posts: 19
Hi All,

I have to make a java program which checks first if the user running the program in UNIX is root or not. I know the command id -u but I don't know how to implement it in the Java program. Any help would be appreciated.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 9138
    
    9

You could run the native program with java.lang.Runtime.exec() or you could check the current user name with java.lang.System.getProperty().

[How To Ask Questions On JavaRanch]
Raf Szczypiorski
Ranch Hand

Joined: Aug 21, 2008
Posts: 383
I think it should be the other way round - you should write a shell script that checks this, and only if the user is root would you call a Java app.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17046
    
  26

There's no way to check for a root user in native Java, since the concept is OS-dependent and therefore excluded under the "write-once/run-anywhere rule". The easiest way to check is to do a Runtime.exec on the "id -u" or "id -g" command. You'll have to capture and parse the number passed back on stdout.


An IDE is no substitute for an Intelligent Developer.
 
Have you checked out Aspose?
 
subject: check UID with Java
 
It's not a secret anymore!