Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting OS environment variables  RSS feed

 
Walter Chen
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, all:

I am having difficulty getting OS environment variables inside a J2EE app. For example, on Windows, the TEMP variable is defined as "C:\temp" -- you can see this by typing "set" on any command prompt window. I want to retrieve it from the java code (running on an app server), but System.getProperties does not return it. What is the best way to get it?

Thanks,
Walter
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.getProperties() is used to retrieve system properties which are different than environment variables.

System.getenv() or System.getenv(String) can be used to retrieve environment variables.
[ June 27, 2006: Message edited by: Scott Johnson ]
 
Walter Chen
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply but getEnv has been deprecated and the API says use getProperty or getProperties, but those do get the OS environment variables that I want.
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's true that getenv(String) was deprecated in Java 1.4.2 (and some earlier versions).

But in Java 1.5 it's no longer deprecated. In fact, Sun added a new method



In Java 1.4.2, Sun did recommend using the System.getProperty() methods instead of getenv(). But the JVM does not make enviroment variables available through System.getProperty(). You must pass the JVM any value to be set as a system property. They can be passed on the JVM command line (with the -D parameter) or set in your app server config.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!