When working with Java and Scala, you might have to set environment variables. For example, compiling the excellent Configgy for Scala requires Java 1.6. How do you set the environment variables so that Scala programs compile with Java 1.6? It’s easy, but there are some gotchas.

Currently (June 2009), the Mac OS (10.5.7) defaults to Java 1.5. You can change this setting using the Java Preferences program in /Applications/Utilities. Drag 1.6 to the top of the list. Run “java -version” from a command line to see which version the system will use. You should go ahead and switch it to use the latest Java, but you’re not done yet.

The Java Preferences panel affects which version of Java is run, but not which JDK is used. For development, you need the correct Java JDK. Set it via JAVA_HOME.

A simple and oft-recommended way to set JAVA_HOME on the Mac is to point it to “/Library/Java/Home/”. However, this location is for the system’s default, which is currently 1.5.

You want to use 1.6, so point to it directly at

/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home

If you follow that path, you’ll see that there’s also a 1.6.0 directory. But selecting 1.6 means that it will point to the latest 1.6.x version. Use 1.6.0 if you need to specify 1.6.0 specifically.

Now, where to set that variable… Common advice is to set it using a shell export such as

export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home

Add that line to your ~/.profile or your ~/.bashrc and you’re done. That will certainly work for any terminal compiling that you do. But it won’t work for programs installed in your /Applications folder that you don’t run from the command line.


To get those applications to ’see’ the environment variable, use the Doug McClure’s free program RCEnvironment. It’s a System Preferences pane that will edit ~/.MacOSX/environment.plist for you. You could just edit the file, but it’s XML, so let RCEnvironment balance the tags for you.

Finally, if you’re using an ant-based build, you can easily see which sdk is being used in the build process by running

$ ant -diagnostics

…and reading the through the output.


Bookmark and Share

One Response to “Understanding Mac environment variables”

  1. Chris Collins says:

    As of java update 4 you can determine a java path that fits your criteria using /usr/libexec/java_home. This can output xml to better integrate with your toolchain and you can specify constraints (such as architecture and intended application).

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>