File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes access the evironment variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "access the evironment variable" Watch "access the evironment variable" New topic
Author

access the evironment variable

Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3479

Hola.
how can I access the environment variable of the OS inside my application ?
Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263
java.lang.System.getProperty


Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

To expand on what Tom said:

Officially, you can't get the environment variables. The Javasoft folks have tried to avoid including things in Java that don't exist on all the platforms Java might be implemented on. There is a method getenv() in the java.lang.System class, but if you call it nowadays, it just throws an exception:


getenv no longer supported, use properties and -D instead.


So what you're supposed to do is use the -D switch to the Java runtime to pass "properties" in which are set to the values of the environment variables of interest; i.e., on Windows, something like:



[Jess in Action][AskingGoodQuestions]
Sol Mayer-Orn
Ranch Hand

Joined: Nov 13, 2002
Posts: 311
I could only answer for java 1.4 (haven't had a look at 1.5 yet).
There's no direct support for obtaining any environment variables. Java system properties are not identical to environment vars. There are some work arounds, non of them are 100 percent portable:

(1) Simplest and most recommended for simple cases: the batch invoking java will extract the environment variable & pass it as a java system property. For instance, passing windows environment variable PATH:


2) Write a c/c++ code that obtained the required environment var, then call it from java using JNI.

3) You could use class Runtime to invoke the operating system's ECHO command. It should be something like Runtime.getRuntime().exec("echo %path%") , but there's some extra work required for reading the output, and I don't remember the details.

4) As previous replies indicated, *some* environment variables (not all) may be automatically supplied by java as a system property. For instance, System.getProperty("java.class.path") will provide the classpath (however, note this is not exactly the same as accessing the CLASSPATH environment variable, since classpath may be determined by "java -classpath...."

Cheers
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: access the evironment variable