This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Accessing system environnement variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Accessing system environnement variables" Watch "Accessing system environnement variables" New topic
Author

Accessing system environnement variables

godbillot samuel
Greenhorn

Joined: Aug 17, 2000
Posts: 2
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.
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
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).

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Bill Compton
Ranch Hand

Joined: Aug 26, 2000
Posts: 186
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...??
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Accessing system environnement variables
 
Similar Threads
Sorry, but i just dont get how to set the PATH variable :sheepish:
Accessing system environnement cariables
can you help me please?: error whtih inheritance
Path and Classpath
PATH & CLASSPATH?