Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading Environment variables in java ..

Status
Not open for further replies.

MaheshRathi

Technical User
Jan 17, 2002
62
IN
Hi,

I want to read some environment variables inside java code. I am not able to use System.getProperty() as it can read only some variables which are predefined. My requirement is -
1. I am declaring a variable and exporting it before execution of code starts.
2. i want to access the value of that variable in my java code.

Appreciate, if someone can provide me some guidelines or some pointers.

thanks in advance,
Mahesh
 
Java does not support reading/writing environment variables as they are OS dependent. You should look at using JNI if you need that functionality.

-pete
 
If the idea behind having these environment variables is just to configure some environment-specific parameters outside the Java code, you can try this instead.

Register the property using -D flag while you invoke your java application. This will be accessible inside java code with System.getProperty(). For instance, if you invoke your application as,

Code:
 java [b]-Dmyvariable=xyz[/b] com.abc.MyApplication
Now System.getProperty("myvariable") will return xyz inside Java code.

Hope this helps!


Ganesh
If you find my post really helpful, please record it by clicking the purple star below.
 
Oops! these TGML tags are killing me. The command-line invokation should read,
Code:
java -Dmyvariable=xyz com.abc.MyApplication

Sorry about that..


Ganesh
If you find my post really helpful, please record it by clicking the purple star below.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top