Accessing Environment Variables

Hi ,

I wrote following code to access Environment Variable "CLASSPATH".

But it is always giving null.

But when doing[ echo $CLASSPATH ] in shell I am getting value.

import java.io.*;

class Li

{

publicstaticvoid main (String[] args)

{

String s = System.getProperty("CLASSPATH");

System.out.println(s);

}

}

[683 byte] By [sharath_535a] at [2007-10-3 9:35:16]
# 1

Java's system properties are not the same as the operating system's variables.

Try:String s = System.getProperty("java.class.path");

You might want to check out the documentation for getProperties here:

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html

pbrockway2a at 2007-7-15 4:50:39 > top of Java-index,Java Essentials,Java Programming...
# 2
HI , Thats working fine with CLASSPATH. But the problem is : I created new environment variable using: setenv file_name /home/test/a.txt How to access the enviromnet variable "file_name" now. java.file_name is not working
sharath_535a at 2007-7-15 4:50:39 > top of Java-index,Java Essentials,Java Programming...
# 3
> java.file_name is not workingNo it won't give anything useful, according to the link I posted. Did you follow that link, by the way? What were the two methods listed just above getProperties()?
pbrockway2a at 2007-7-15 4:50:39 > top of Java-index,Java Essentials,Java Programming...
# 4

Yes I tried above two methods also

String s = System.getProperty("file_name");giving null as output

String s = System.getProperty("file_name"); is giving followig compilation error :

Note: Li.java uses or overrides a deprecated API.

Note: Recompile with -deprecation for details.

When I run itgiving following error :

Exception in thread "main" java.lang.Error: getenv no longer supported, use properties and -D instead: input

at java.lang.System.getenv(System.java:691)

at Li.main(Li.java:6)

sharath_535a at 2007-7-15 4:50:39 > top of Java-index,Java Essentials,Java Programming...
# 5
[edit] Massive edit - didn't read the last post properly.I think the getenv(String) was deprecated in 1.4 (for somereason), but then undeprecated in 1.5.It should be OK to sayString s = System.getenv("file_name");
pbrockway2a at 2007-7-15 4:50:39 > top of Java-index,Java Essentials,Java Programming...
# 6
I think you meant to say that System.getenv("file_name"); is giving the following compilation error.It's just a warning, not an error. It should compile and run OK so long as you don't use the -deprecation flag.
pbrockway2a at 2007-7-15 4:50:39 > top of Java-index,Java Essentials,Java Programming...
# 7
> It should compile and run OKBut check out reply 9 in this thread... http://forum.java.sun.com/thread.jspa?threadID=568871&messageID=2811733Are you using a version of java that throws this error?
pbrockway2a at 2007-7-15 4:50:39 > top of Java-index,Java Essentials,Java Programming...
# 8

Hi ,

I triled that one also.

String s = System.getenv("file_name");

Compilation error is coming. I ignore that when I run that I am gettinh following error:

Exception in thread "main" java.lang.Error: getenv no longer supported, use properties and -D instead: input

at java.lang.System.getenv(System.java:691)

at Li.main(Li.java:6)

sharath_535a at 2007-7-15 4:50:39 > top of Java-index,Java Essentials,Java Programming...
# 9
Try running with the -D switchjava -Dli.file.name=yourvar LiInside your program you can access the property "li.file.name" with System.getProperty().You would replace "yourvar" with something that your OS would replace with thefile_name environment variable.
pbrockway2a at 2007-7-15 4:50:39 > top of Java-index,Java Essentials,Java Programming...
# 10

> Compilation error is coming.

There is a difference between a compiler error and a compiler warning.

If you are getting an error then either your code is wrong or you are using a very non-standard compiler.

A warning means that the class file was created. Whether it works (runtime) at that point depends on the version of java that you are using.

1.3 and 1.5 will work. 1.4 will not.

jschella at 2007-7-15 4:50:39 > top of Java-index,Java Essentials,Java Programming...
# 11

Here's a Windows example - I don't have a 1.4 compiler/runtime handy to test it.public class VarEg {

/*

* javac -cp . VarEg

*

* set eg_var=foo

* java -cp . -Deg.var=%eg_var% VarEg

*/

public static void main(String[] args) {

// output: var=foo

System.out.println("var=" + System.getProperty("eg.var"));

}

}

pbrockway2a at 2007-7-15 4:50:39 > top of Java-index,Java Essentials,Java Programming...
# 12
I dont find any way to access user defined environments till now.in replies i got some solutions but all are givng null as output.
sharath_535a at 2007-7-15 4:50:39 > top of Java-index,Java Essentials,Java Programming...
# 13

> I dont find any way to access user defined

> environments till now.

> in replies i got some solutions but all are givng

> null as output.

Then you are doing something wrong. Since you didn't provide information on what you attempted it isn't possible to say what is wrong.

jschella at 2007-7-15 4:50:39 > top of Java-index,Java Essentials,Java Programming...