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);
}
}
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
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
> 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()?
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)
[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");
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.
> 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?
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)
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.
> 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.
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"));
}
}
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.
> 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.