• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get the system variable in UNIX?

 
Jamy Wang
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I get the system variables in UNIX?
I use Runtime.getRuntime().exec("echo $JAVA_HOME"), but it doesn't work.
Thank you in advance. Could anyone give me a complete example?
 
James Swan
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can set properties from environment variables when you invoke the java interpreter.
e.g from *nix

or from windows

and then from within MyClass.java do something like:
 
James Swan
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also, as an FYI you can get $JAVA_HOME from:
System.getProperty("java.home");
But the above stands for other non-java related env. variables.
 
Jamy Wang
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, but I can't pass the properties by -D.
So I should get the runtime system variable.
Could anyone tell me how to do it on UNIX?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no (undeprecated or working) API that sets or gets environment variables in Java.
You original attempt, where you used "echo", is reasonable -- you'd just have to work out the details and do it correctly.
 
Jamy Wang
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Ernest.
I agree with you. But I just can not get the correct result. Could you give me an example? Thank you very much!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Off the top of my head:
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You also may find this article helpful. Runtime's exec() can be difficult to debug when it doesn't work, and the article covers many of the common problems.
[ January 13, 2004: Message edited by: Jim Yingst ]
 
E Weibust
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got an easier way. Look at this:
public class Tester {
public static void main(String[] args) {
System.out.println(System.getProperty("testEnvVal"));
}
}
The String testEnvVal is the Env val you want.
Then you can read it from the system by using:
java -DtestEnvVal=%testEnvVal% Tester
java -DtestEnvVal=$testEnvVal Tester
I hope that helps.
Erik
 
Kartik Ruppa
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
The following link gives u the details for getting env variables
http://www.rgagnon.com/javadetails/java-0150.html
Thanks,
Kartik
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic