Originally posted by Nitin Dubey: How to access system environment variable in JDK 1.3 ? later API's do asks us to use System.getenv() and works well. But in 1.3 this method is deprecated.
The alternative is suggested in the API, as I quote:
Deprecated. The preferred way to extract system-dependent information is the system properties of the java.lang.System.getProperty methods and the corresponding getTypeName methods of the Boolean, Integer, and Long primitive types. For example:
Try this code out then (it gets the classpath environment variable):
Is that a bit clearer?
This information is in the API JavaDocs. If you look at the documentation for the getProperties() method for the System class, the key values for the system properties that can be retrieved are listed.
Regards, JD [ February 15, 2006: Message edited by: John Dell'Oso ]
Joined: Apr 02, 2004
Originally posted by Nitin Dubey: I didn't quite get that. like what kind of method they want us to call ? and what actually happens ?
I tried to copy paste the code and run it but it won't compile due to the foreign method.
What do you mean by foreign method? The method is provided for in the System class. Did you put the codes in a class like what John did? You should be able to compile it. It is essentially doing what you wanted it to do, accessing the system environment variable.
Originally posted by Chengwei Lee: It is essentially doing what you wanted it to do, accessing the system environment variable.
Well, but it's not, actually. It's accessing properties that are pre-set, or that are set by JVM command-line arguments. An arbitrary environment variable is not -- indeed, no environment variable is -- going to be available via System.getProperty(). For the FOO environment variable to be available via System.getProperty(), Java has to be launched like
java -DFOO=$FOO MyClass
(on a UNIX system; on Windows, the syntax is different.) Sometimes this is an option, but sometimes it's not.
There was and is precisely one portable way to get the values of environment variables using the standard Java API: using getenv(). For several releases, getenv() didn't work, so you're out of luck. It works in Tiger again, which is a relief.
As to nonportable ways to do it: you can use your system's shell to probe the environment. For example, on a UNIX-alike, you can execute a command like "echo $PATH" using Runtime.exec() to get the PATH.
I tried to run the code as mentioned in the 1.3 API that didn't run. I was unable to understand the reason why the have used "enableExpertCommands" method and what is it meant for.
Well, the actually problem remains the same. I want to access OS Environment Variable from a 1.3 java class. As Ernest has said there are methods to do this in OS dependent manner. But I am looking for a solution that is platform independent.
getEnv() method was deprecated in 1.3 API. And since it was deprecated there has to be some other method through which we can access these variables. But the example they have shown is to access Java Environment Variables. Hence this confusion. Apparantly, the method was undeprecated in 1.5
Thanks Ernest Friedman-Hill for making the things clearer. Actually, that's what I should have written in my first post.