wood burning stoves*
The moose likes Java in General and the fly likes Accessing Environment Variable in JDK 1.3 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Accessing Environment Variable in JDK 1.3" Watch "Accessing Environment Variable in JDK 1.3" New topic
Author

Accessing Environment Variable in JDK 1.3

Nitin Dubey
Ranch Hand

Joined: Oct 30, 2000
Posts: 126

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.

Cheers,

Nitin
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
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:


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
Nitin Dubey
Ranch Hand

Joined: Oct 30, 2000
Posts: 126

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.

Cheers,

Nitin Dubey
John Dell'Oso
Ranch Hand

Joined: Apr 08, 2004
Posts: 130
Nitin,

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 ]
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
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.

Cheers,

Nitin Dubey


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.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
Thanks for correcting me.

So it all depends on what Nitin needs to retrieve then? Whether its pre-set or not environment variables.
Nitin Dubey
Ranch Hand

Joined: Oct 30, 2000
Posts: 126

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.

Cheers,

- Nitin
 
wood burning stoves
 
subject: Accessing Environment Variable in JDK 1.3