How to read Environment Variable in Java.
Hi,
I am trying to read a environment variable $TMP which has a value of /apps/bea/pt847/psoft5/tmp in UNIX.
I have written the following code and getting Null pointer exception when calling the method logmessage. Any help with reading environment variable is appreciated.
publicstaticvoid logMessage(String message){
SimpleDateFormat formatter =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
Date now =new Date();
Properties p =null;
try
{
p = getEnvVars("$TMP");
System.out.println("the current value of TEMP is : " + p.getProperty("$TMP"));
}
catch (Throwable e){
e.printStackTrace();
}
try
{
BufferedWriter os =new BufferedWriter(new FileWriter("/tmp/pscas_signon_log.txt",true));
os.write(formatter.format(now));
os.write(message);
os.write("\n");
os.write("tmp_env = " + p.getProperty("$TMP"));
os.write("\n");
os.close();
}
catch(IOException _ex){}
}
publicstatic Properties getEnvVars(String env_var)throws Throwable{
Properties envVars =new Properties();
Process p = Runtime.getRuntime().exec(env_var);
BufferedReader br =new BufferedReader(new InputStreamReader( p.getInputStream() ) );
String line =null;
while( (line = br.readLine()) !=null ){
int idx = line.indexOf('=' );
String key = line.substring( 0, idx );
String value = line.substring( idx+1 );
envVars.setProperty( key, value );
// System.out.println( key + " = " + value );
}
return envVars;
}
String s = System.getenv("TMP");[EDIT] Correction. Sorry for the original misleading, incorrect answer.~
"$TMP" is not an executable. I'm sure if you search the forums, you'll find plenty of threads on this topic
System.getProperty("$TMP") does not work. This is where I am getting Null pointer Exception. Is there a way I can pass command line parameter in PeopleCode or read the $TMP environment variable in Java code.
> System.getProperty("$TMP") does not> work. This is where I am getting Null pointer> Exception. That line is not in the code you posted, so there is no way you can get a NullPointerException from it.~
I changed the code based on previous post.
> I changed the code based on previous post.Do share please.
> Is there a way I can pass command line parameter in PeopleCode
Yes, if PeopleCode has a main() method or the main() method of some other class passes the argument.
> or read the $TMP environment variable in Java code.
Yes, the System.getProperty() method will return the value of $TMP if such a variable exists.
Correction: the System.getenv() method will return the value of TMP if such a variable exists.
~
> I changed the code based on previous post.And you're keeping your changes a secret because...?~
Updated code:
......
public static void logMessage(String message) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
Date now = new Date();
Properties p = null;
try
{
p = getEnvVars("$TMP");
System.out.println("the current value of TEMP is : " + System.getProperty("$TMP"));
}
catch (Throwable e) {
e.printStackTrace();
}
try
{
BufferedWriter os = new BufferedWriter(new FileWriter("/tmp/pscas_signon_log.txt", true));
os.write(formatter.format(now));
os.write(message);
os.write("\n");
os.write("tmp_env = " + System.getProperty("$TMP"));
os.write("\n");
os.close();
}
catch(IOException _ex) { }
}
public static Properties getEnvVars(String env_var) throws Throwable {
Properties envVars = new Properties();
Process p = Runtime.getRuntime().exec(env_var);
BufferedReader br = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
String line = null;
while( (line = br.readLine()) != null ) {
int idx = line.indexOf( '=' );
String key = line.substring( 0, idx );
String value = line.substring( idx+1 );
envVars.setProperty( key, value );
// System.out.println( key + " = " + value );
}
return envVars;
}
and this is the error message..
java.io.IOException: $TMP: not found
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:72)
at java.lang.Runtime.execInternal(Native Method)
at java.lang.Runtime.exec(Runtime.java:602)
at java.lang.Runtime.exec(Runtime.java:461)
at java.lang.Runtime.exec(Runtime.java:397)
at java.lang.Runtime.exec(Runtime.java:359)
at SimpleCasValidator.getEnvVars(SimpleCasValidator.java:108)
at SimpleCasValidator.logMessage(SimpleCasValidator.java:81)
public static void logMessage(String message) {
System.out.println("the current value of TEMP is : " + System.getenv("TMP"));
// the rest of your code...
}
EDIT: I gave the wrong method. Sorry for any confusion.
~
> Hi,> I am trying to read a environment variable $TMP which> has a value of /apps/bea/pt847/psoft5/tmp in UNIX.You do NOT have that variable on your system.However, you do have a variable named TMP (no $) on your system.
System.getProperty() does NOT provide access to environment variables. It is used to access Java system properties that are passed to the VM using the -D attribute, or set inside the code.
System.getenv() provides access to environment variables. As jschell mentioned, environment variables should be referenced without the $ sign.