• 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

Accessing system environnement variables

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.
I'm try to access to some system environnement variable, like $TERM,$PATH, $LD_LIBRARY_PATH or user define variables.
How can I do that ?
I can do it with something like:
Runtime.getRuntime().exec("echo $PATH")
but it's not very efficient, and not very clean.
Thx.
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use the method getProperty in the System class.
String path = System.getProperty("PATH");
This works for any environment variable you have set. The method throws a SecurityException if a SecurityManager is set and assess isn't granted to the System parameters (such as when run in an Applet).
 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm looking for a way to get environment variables into a java app and am really surprised at how challenging this is proving. I tried the above code thus:
But System.getProperty(...) returns null for everything I've tried -- with and without the "%" for NT and the "$" for Unix, etc. Am I doing it wrong? Surely something as fundamental as getting env var's isn't unsupported...??
 
He was expelled for perverse baking experiments. This tiny ad is a model student:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic