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+" = <" + envVars.get(varName)+">");<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!
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.