Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

check UID with Java

 
Nikolay Tsonkov
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9214
9
Linux Mac OS X Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could run the native program with java.lang.Runtime.exec() or you could check the current user name with java.lang.System.getProperty().
 
Raf Szczypiorski
Ranch Hand
Posts: 383
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 17622
39
Android Eclipse IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic