Meaningless Drivel is fun!
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
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: 9190

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: 17421

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.
I agree. Here's the link:
subject: check UID with Java
It's not a secret anymore!