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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Windows Environment Variables in Java

Status
Not open for further replies.

stevensteven

Programmer
Jan 5, 2004
108
0
0
CA
Hello,

In my java code, I would like to store a windows environment variable into a variable I use in my java code. Is there a built in call for this?

Thanks for any help,

Steven
 
No, because Java is OS independant.Why do you need to do this ? There are probably neater ways of getting around the problem ...
 
I need a certain windows environment variable to use in my java program.

I can get it at the command prompt by typing "SET userdomian" to get my required information.
 
If you really must (though I would advise finding another way around your problem), you could use this code :

Code:
Process p = Runtime.getRuntime().exec("set userdomain");
int exitStatus = p.waitFor();
if (exitStatus != 0) {
  throw new IOException("Process exited with error");
}

InputStream is = p.getInputStream();
// The read your IO stream as you would any other stream
 
Of course, as idarke says you can pass variables to the JVM via "java -Dmy.var=%MY_VAR%" , but I don't believe this is what the guy wanted ... could be wrong ?!!
 
I thought System.getProperty() (or something like that) worked in general for Environment Variables as the JVM is still compiled for each platform??
 
System.getProperty() will retrieve variables that are set with a "-D" switch, and also standard JVM properties such as "java.endorsed.dirs", but these have nothing to do with OS level environment variables.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top