by Tony Sintes

Retrieve environment variables: Color me deprecated

news
Jul 6, 20013 mins

To retrieve environment variables from a Linux system, you'll need something besides getenv()

Q: How can I read an environment variable on a Linux system from a Java application?

A: In the past you could use System.getenv("env-var name") to retrieve environment variables. However, that now deprecated method will throw the following exception if you attempt to call it:

Exception in thread "main" java.lang.Error: getenv no longer supported, use properties and -D instead: PATH at java.lang.System.getenv(System.java:677) at Test.main(Test.java:4)

System.getProperty() now serves as the preferred way to retrieve values your program may need. Indeed, the getenv()‘s deprecation has caused much contention, as seen in this bug report on Sun’s Java Developer’ Connection (requires free login):

http://developer.java.sun.com/developer/bugParade/bugs/4199068.html

Sun decided to deprecate the method because environment variables are a platform-specific feature, and some platforms, MacOS for example, simply do not have the concept of an environment variable.

You have a few options for reading environment variables; however, each will call for a little extra work on your part.

System.getProperty() will retrieve the Java-defined system properties. You can use the following code to determine the available values:

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() );
     }
 }

On my system, I get the following list of values:

java.runtime.name
sun.boot.library.path
java.vm.version
java.vm.vendor
java.vendor.url
path.separator
java.vm.name
file.encoding.pkg
java.vm.specification.name
user.dir
java.runtime.version
java.awt.graphicsenv
os.arch
java.io.tmpdir
line.separator
java.vm.specification.vendor
java.awt.fonts
os.name
java.library.path
java.specification.name
java.class.version
os.version
user.home
user.timezone
java.awt.printerjob
file.encoding
java.specification.version
java.class.path
user.name
java.vm.specification.version
java.home
user.language
java.specification.vendor
awt.toolkit
java.vm.info
java.version
java.ext.dirs
sun.boot.class.path
java.vendor
file.separator
java.vendor.url.bug
sun.io.unicode.encoding
sun.cpu.endian
user.region
sun.cpu.isalist

To retrieve one of these values, such as java.class.path, you can say System.getProperty("java.class.path"). If a value you need does not appear automatically in the system properties, you can add it through the -D command line option:

java -DENV_VARIABLE_NAME=$ENV_VARIABLE_NAME ClassName

You must add a -D entry for each value you need to pass to your program. If you have a large number of values, you’ll want to write a script that starts up the program. It’ll save you a lot of typing!

Alternatively, you can also pipe the return of env to a file. You can then load that file using a Properties object within your program. (You can also exec env from within your program. However, env does not exist on every platform)