Click to See Complete Forum and Search --> : System environment variables


giorgio
September 23rd, 1999, 09:11 AM
Hi,

I would like to get the value of some system environment variables, but can't get it using the java.lang.System class (method "public static String getProperty(String key)"). This class just allow me to get the command line parameters passed to the JVM.

How can I get the system environment variables?

Thanks for any suggestion.

amaresh
October 7th, 1999, 01:11 PM
Did you check the other two fucntions in that class? Viz., getProperties() and getProperty(String key, String def)? getProperties() returns an instance of "Properties" and contains all the system parameters like java.version, OS name, version etc etc.
Please let me know if that's suitable..
Thanks,
Amaresh

giorgio
October 8th, 1999, 05:04 AM
It makes no difference doing System.getProperties().getProperty("MY_ENV_VAR") or System.getProperty("MY_ENV_VAR"). In addition, System.getProperty(String key, String def) just initialize the value associatet to key if it isn't defined.
The class java.lang.System just allow to get the command line parameters passed to the JVM and some JVM defined environment variables "like java.version, OS name, version etc etc.".

poochi
October 8th, 1999, 07:21 AM
There is not pure java solution to get system dependent variables. you have to write
native code for this. I dont know about JNI. There might be some solution in there.

janvi
November 29th, 1999, 11:20 AM
Hi,

I have the same need to get the system variable value which is not system property.
please let me know if you the solution .
question:



Hi,

I am trying to get the environment variavle value
(ex: variable : xx
value : trytyy
which is set manually in Start/control panel/system/environment/System variables in NT machine.How to get this value in servlet.

________________________________
my environment is
jdk1.2
javawebserver1.1.3
NT osystem
jsdk2.0
________________________________


I found the getenv(String) function but its deprecated function top of that its not working (error in run time : server returned a invalid and unrecognized response.............).

I tried getPropert(),getProperties()................but no way.


Thanks

poochi
November 29th, 1999, 12:06 PM
Sorry for my misleading answer in the other post..
You can use the following class to get the environment variable..
This is for windows specific..


import java.io.*;
import java.util.*;

public class EnvironmentVariables extends Hashtable{

public EnvironmentVariables() {
String[] cmd = {"cmd", "/c", "SET"};
try {
Process proc = Runtime.getRuntime().exec(cmd);
BufferedReader inp = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String LINE = inp.readLine();
while (LINE != null) {
int eqPos = LINE.indexOf("=");
if (eqPos >= 0) {
String name = LINE.substring(0, eqPos);
String value = LINE.substring(eqPos + 1);
super.put(name.toUpperCase(), value);
}
LINE = inp.readLine();
}
inp.close();
} catch (IOException e) {
System.out.println("Weird I/O error");
}
}

public synchronized String get(Object name) {
String theName = name.toString().toUpperCase();
return (String)super.get(theName);
}

public synchronized Object put(Object name, Object value) {
String theName = name.toString().toUpperCase();
String theValue = value.toString();
Object result = super.get(theName);

// this is windows specific
String[] cmd = {"cmd", "/c", "SET " + theName + "=" + theValue};
try {
Process proc = Runtime.getRuntime().exec(cmd);
BufferedReader inp = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String LINE = inp.readLine();
while (LINE != null) {}
super.put(name.toString().toUpperCase(), value.toString());
return result;
} catch (IOException e) {
System.out.println("Weird I/O error");
return null;
}
}
}





Usage :


EnvironmentVariables eVariables = new EnvironmentVariables();
String value = eVariables.get("MYENVIRONMENTVARIABLE");