The moose likes Java in General and the fly likes java to read System environment variables!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "java to read System environment variables!!!" Watch "java to read System environment variables!!!" New topic

java to read System environment variables!!!

Subhadip Chatterjee
Ranch Hand

Joined: Dec 12, 2006
Posts: 93
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.

Refreshing life every moment...
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 407

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 ..


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24183

Moving to Java in General (Intermediate).

[Jess in Action][AskingGoodQuestions]
Subhadip Chatterjee
Ranch Hand

Joined: Dec 12, 2006
Posts: 93
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
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
You can use System Properties. Try google search.
Jan Cumps

Joined: Dec 20, 2006
Posts: 2491

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 ]

OCUP UML fundamental and ITIL foundation
youtube channel
I agree. Here's the link: http://aspose.com/file-tools
subject: java to read System environment variables!!!
Similar Threads
How to run .jar if pc doesnt have the java library environment variables set
easier ways to get java to work for non technical users...
ClassNotFoundException: com.mysql.jdbc.Driver
system level classpath, application level classpath, classloader
Java Desktop system