GeeCON Prague 2014*
The moose likes Java in General and the fly likes How to get the system variable in UNIX? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "How to get the system variable in UNIX?" Watch "How to get the system variable in UNIX?" New topic
Author

How to get the system variable in UNIX?

Jamy Wang
Ranch Hand

Joined: Sep 21, 2003
Posts: 83
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?


Best Regards<br /> <br />SCJP1.4 SCJD1.4 SCEA
James Swan
Ranch Hand

Joined: Jun 26, 2001
Posts: 403
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

Joined: Jun 26, 2001
Posts: 403
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

Joined: Sep 21, 2003
Posts: 83
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Jamy Wang
Ranch Hand

Joined: Sep 21, 2003
Posts: 83
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

Off the top of my head:
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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 ]

"I'm not back." - Bill Harding, Twister
E Weibust
Ranch Hand

Joined: Jun 13, 2003
Posts: 54
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


---<br />Erik Weibust<br /><a href="http://erik.weibust.net" target="_blank" rel="nofollow">http://erik.weibust.net</a>
Kartik Ruppa
Ranch Hand

Joined: Aug 28, 2002
Posts: 60
hi,
The following link gives u the details for getting env variables
http://www.rgagnon.com/javadetails/java-0150.html
Thanks,
Kartik
 
Consider Paul's rocket mass heater.
 
subject: How to get the system variable in UNIX?