• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to get the system variable in UNIX?

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Off the top of my head:
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
What does a metric clock look like? I bet it is nothing like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic