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;

}

[3090 byte] By [Jay_Amballaa] at [2007-11-27 4:50:05]
# 1
String s = System.getenv("TMP");[EDIT] Correction. Sorry for the original misleading, incorrect answer.~
yawmarka at 2007-7-12 10:03:19 > top of Java-index,Java Essentials,Java Programming...
# 2
"$TMP" is not an executable. I'm sure if you search the forums, you'll find plenty of threads on this topic
bsampieria at 2007-7-12 10:03:19 > top of Java-index,Java Essentials,Java Programming...
# 3
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.
Jay_Amballaa at 2007-7-12 10:03:19 > top of Java-index,Java Essentials,Java Programming...
# 4
> 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.~
yawmarka at 2007-7-12 10:03:19 > top of Java-index,Java Essentials,Java Programming...
# 5
I changed the code based on previous post.
Jay_Amballaa at 2007-7-12 10:03:19 > top of Java-index,Java Essentials,Java Programming...
# 6
> I changed the code based on previous post.Do share please.
cotton.ma at 2007-7-12 10:03:19 > top of Java-index,Java Essentials,Java Programming...
# 7

> 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.

~

yawmarka at 2007-7-12 10:03:19 > top of Java-index,Java Essentials,Java Programming...
# 8
> I changed the code based on previous post.And you're keeping your changes a secret because...?~
yawmarka at 2007-7-12 10:03:19 > top of Java-index,Java Essentials,Java Programming...
# 9

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)

Jay_Amballaa at 2007-7-12 10:03:19 > top of Java-index,Java Essentials,Java Programming...
# 10

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.

~

yawmarka at 2007-7-12 10:03:19 > top of Java-index,Java Essentials,Java Programming...
# 11
> 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.
jschella at 2007-7-12 10:03:19 > top of Java-index,Java Essentials,Java Programming...
# 12

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.

Herko_ter_Horsta at 2007-7-12 10:03:19 > top of Java-index,Java Essentials,Java Programming...