Aug 21 2009

How to read environment variables in Java

Published by michael 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

or

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");
Share and Enjoy:
  • Print
  • Sphinn
  • del.icio.us
  • Mixx
  • Google Bookmarks
  • Blogplay
  • Technorati
  • Twitter

Related posts:

  1. Java and the current work directory I spent some time looking this up and hopefully I...

No responses yet

Trackback URI | Comments RSS

Leave a Reply