aspose file tools*
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 Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "check UID with Java" Watch "check UID with Java" New topic

check UID with Java

Nikolay Tsonkov

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

Joined: Oct 29, 2001
Posts: 8927

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

"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[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: 16142

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.

Customer surveys are for companies who didn't pay proper attention to begin with.
Don't get me started about those stupid light bulbs.
subject: check UID with Java