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!

environment variables

Status
Not open for further replies.

toff

Programmer
May 13, 1999
1
CH
I want to read environment variables. The function System.getenv(...) does not work because it is deprecated. There's a hint in the source, that I should use properties instead. How does it works?<br>
regards toff
 
Hi!<br>
<br>
The environment variables are operating system dependets,<br>
thus no platform-portable java method to read them.<br>
You may use the -D to define property when execute the class<br>
with java.exe or see my java source below, that prints all<br>
environment variables.<br>
The exec string is system dependent :)), I use WinNT.<br>
On Win9x: "command /c set"<br>
On unix: "/bin/env"<br>
<br>
*********************<br>
import java.io.*;<br>
import java.util.*;<br>
<br>
public class GetEnv {<br>
public static void main(String[] args) {<br>
Properties envVars = new Properties();<br>
Enumeration e;<br>
String varName;<br>
<br>
try {<br>
envVars.load(<br>
Runtime.getRuntime().exec("cmd /c set").getInputStream()); <br>
} catch (Throwable t) {t.printStackTrace();}<br>
<br>
for (e=envVars.propertyNames(); e.hasMoreElements(); ) {<br>
varName=(String) e.nextElement();<br>
System.out.println("\n"+varName+" = &lt;" + envVars.get(varName)+"&gt;");<br>
}<br>
}<br>
}<br>
*********************<br>
Have a nice Java!<br>

 
You can convert platform-specific environment variables into JVM environment properties on the command line.<br>
It would be something like (on Windows):<br>
<br>
java -Dmy.jvm.var=%MY_WINDOW_VAR% myClass<br>
<br>
For this execution the System properties will include an entry for the key "my.jvm.var". Use a "-D" for each variable/property. Better write a batch file!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top