Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Getting OS environment variables

 
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
 
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.
 
This will take every ounce of my mental strength! All for a tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic