Web Development

General discussion


To read Environment Variables in java

By kellogy22 ·
How to read the environment variable that is set in the windows os, inside the java program?(Environment variables such as CATALINA_HOME,ANT_HOME etc..)

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Comments

Collapse -

by rashmigopinath In reply to To read Environment Varia ...

You can retrieve the Java-defined system properties in your code using the
You can use the following code to determine which are the Java-defined system
properties on your machine:
public static void main( String [] args ) {
java.util.Properties p = System.getProperties();
java.util.Enumeration keys = p.keys();
while( keys.hasMoreElements() ) {
System.out.println( keys.nextElement() );

If you have read the value of an environment value set on your Windows OS (for
example the value of path variable), then you would have to do the following:

public static void main(String[] args)
String path = System.getProperty("path");
System.out.println("the value of path is "+path);

While running the above code, you would have to run it as:
java -Dpath="%path%" myClassname

Thus, whatever environment variable that you want to read in your java file, you
would need to specify that in the java command while running the file.
java -Dpath="%path%" -Dclasspath="%classpath%" myClassname

Do let me know if you have any more questions.


Collapse -

by kellogy22 In reply to

Poster rated this answer.

Collapse -

by kellogy22 In reply to To read Environment Varia ...

This question was closed by the author

Related Discussions

Related Forums