Aug 21 2009

How to read environment variables in Java

Published by at 10:50 pm under Java

You can set environment variables or pass them in using the -Dvariablename syntax

SET logfile=/location/to/my/log/file.log


java -Dlogfile=/location/to/my/log/file.log HelloWorld

When you want to access these environment variables from your Java source file you can use

Java 1.5 and newer:

String logfileLocation = System.getenv("logfile");

Java 1.4 and older:

String logfileLocation = System.getProperty("logfile");

No responses yet

Leave a Reply