Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enviroment Variables

 
Rob Bass
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since SUN has deprecated getenv(), does anyone know how to retrieve a user set enviroment variable?
Thanks,
Rob
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JDK does not provide a way to get environment variables, because they are not a concept common to all platforms. Instead, it provides java.util.Properties . One way to get environment variables into your Java program is to set Java property values to be equal to the environment variable values on the java command line; use the syntax for your platform. For example: -
<code>
java -DmyProp=$MY_PROP myClass
java -DmyProp=%MY_PROP% myClass
</code>
There are standard Java properties for some things you may want to get, such as working directory, home directory, OS name etc. See System.getProperties().
Alternatively, if you really must access actual environment variables, you can make a teeny C program to get their values, and call this via JNI. I suspect that a search on the Web will probably reveal several "how to" documents on this subject, as it must be quite a common requirement.
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just saw a JDC Tech Tip on this one
ACCESSING THE ENVIRONMENT FROM JAVA APPLICATIONS
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
Co-author Mike Meyers' Java 2 Certification Passport
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic