• Post Reply Bookmark Topic Watch Topic
  • New Topic

Coding for Enviorment variables

 
Chris Gastin
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an application that when it starts up it reads a .config file.

Currently the path is hard coded, but I would like to read that path from an enviorment variable called "FTPSPIDER_HOME" which is set to "C:\FTPSpider\".
I have tried this but I get an runtime exception saying that it can not find the file.

Any help with reading from an enviorment variable would be greatly appreciated
Thanks
Chris Gastin
SCJP 1.4
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You used to be able to use System.getenv() but that method is deprecated because it is not portable since not all operating systems have the concept of an env variable. Instead, the api recommends using System.setProperty() and System.getProperty().
As another alternative (way around it), this is what we do...
All of our apps are started by a script. The script has easy access to env variables. The path/filename of the config file is then passed to the java app as an argument. That way, its the script that translates the env variable instead of having the Java app do it.
For example on unix:

Where "$CONF_DIRECTORY" is the env variable that translates to the cofig file directory.
Hope that helps.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!