• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java to read System environment variables!!!

 
Subhadip Chatterjee
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks,
I have a JAR application, that automatically runs by a system scheduler created by me. Now, this JAR application creates a connection to a Oracle DB everytime it runs. or creating the connection,I have hardcoded the JDBC-URL string, schema-name, and password inside the main class. Instead of hardcoding, can't I just set some System environment variables, and read it from Java? If it's possible, let me know how!!!

Give me example for UNIX, as well as Windows system.
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Normally I add a property file to my build and load it relative to the path used by the classloader for Main i.e. you can load it from within the jar when you deploy or from a directory when in dev (same code loads either).

You can do system properties instead but this seems neater, i.e. its all contained in the jar certainly a step up from had coding withing the class, if you don't want to do that you could just add them as command line arguments for instance.

Some stuff on it here, its done a lot so should be all over the web ..

http://forum.java.sun.com/thread.jspa?threadID=254048&messageID=959478
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Java in General (Intermediate).
 
Subhadip Chatterjee
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Chris,
thanks for that link...actually I have already thought about that option, but I thought let's check out the other option,if any.But it's alright.Thanks for the reply.
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use System Properties. Try google search.
 
Jan Cumps
Bartender
Posts: 2586
11
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Subhadip Chatterjee:
Hi Folks,
...
Give me example for UNIX, as well as Windows system.


I pass them via the command line, using the -D option.
If my database info is defined in the operating system property db, I use:

On Unix:


On Windows:


In java, the property can be retrieved by this code:



Java also has the function
It was deprecated in java 1.4, but isn't anymore since java 1.5.


Regards, jan
[ January 29, 2007: Message edited by: Jan Cumps ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic