OS X Java Versions
OS X uses a weird and wonderful structure for Java components that allows multiple versions of Java to coexist happily on the same system. Depending on what you want to do, choosing between them can create challenges.
Apple references that were useful compiling this article:
- Official Apple Java downloads (requires free ADC membership, MacDev center)
- Java FAQ
- Java info.plist reference
Thanks also go to various others who have already plumbed the mysteries of OS X and Java.
- Selecting Java Version – Part 1 and Selecting Java Version – Part 2.
- Fixing Java on Mac Snow Leopard
- How to change the default version of JDK on the Mac
- Snow Leopard Java problems and fix
OS X Directory Structure
OS X places all kinds of Java components in the directory structure starting at:
/System/Library/Frameworks
The component that’s of particular interest for this discussion is the JavaVM itself. That structure starts at:
/System/Library/Frameworks/JavaVM.framework
Each distinct Java version has a directory in
/System/Library/Frameworks/JavaVM.framework/Versions
Here’s a sample from my machine
lrwxr-xr-x 1 root wheel 5B Aug 31 21:35 1.3 -> 1.3.1 drwxr-xr-x 3 root wheel 102B Jul 20 19:35 1.3.1 lrwxr-xr-x 1 root wheel 5B Nov 26 03:43 1.5 -> 1.5.0 drwxr-xr-x 8 root wheel 272B Jan 2 2007 1.5.0 lrwxr-xr-x 1 root wheel 5B Aug 31 21:35 1.6 -> 1.6.0 drwxr-xr-x 8 root wheel 272B Oct 18 08:31 1.6.0 drwxr-xr-x 9 root wheel 306B Oct 18 08:31 A lrwxr-xr-x 1 root wheel 1B Aug 31 21:35 Current -> A lrwxr-xr-x 1 root wheel 3B Aug 31 21:35 CurrentJDK -> 1.6
There are several interesting points about the subdirectories managed here.
- The primary release (e.g ’1.5′) is always a link to the corresponding current ‘dot’ or ‘subrelease’ for that version (e.g ’1.5.0′)
- ‘CurrentJDK’ is linked to whatever is the currently active Java JDK Version
- ‘Current’ is linked to the currently selected Java SE version
There are several levels of indirection involved. You can stop here if you don’t need a further information or a bad headache. In this excursion, I’ll deal with the 1.6 (Java 6) trajectory.
- ‘./Current’ is linked to ‘./A’
- ‘./A’ appears to encompass both Java SE elements and Java JDK elements
- ‘./A/Commands’ appears to contain JSE binaries
- ‘./A/Headers’ appears to contain JDK headers
- ‘./1.6.0′ links to ‘/System/Library/Frameworks/JavaVM.framework/Home’
- ‘Home’ appears to have JDK binaries fot the specified (default?) version (debug versions?)
This all serves to set the scene for the most interesting link from the point of view of daily use – ‘/Library/Java/Home’:
/Library/Java/Home -> /System/Library/Frameworks/JavaVM.framework/Home /System/Library/Frameworks/JavaVM.framework/Home -> /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home
This is the directory that Apple recommends using to set the ‘JAVA_HOME’ variable – beloved of the Java world, and required to get almost anything Java-based to work.
export JAVA_HOME=/Library/Java/Home
export PATH=${JAVA_HOME}/bin:${PATH}
Java Versions
Changes that affect everything on the system
If you want to change the Java version for everything on the system, you can do so with the Java Preferences application
/Applications/Utilities/Java Preferences
On the ‘General’ tab, follow the instructions to change the selection.
If the version you want doesn’t appear on the list, you need something more radical (To be supplied).
Changes that affect the specific command you’re running
The Apple-sanctioned way to run a specific Java version from the command line is simple. This deliberately does not involve changing the Java Preferences.
These changes shown below will only apply for the lifetime of the (bash) shell in which they are used. Using the setup illustrated above for my machine:
export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.5/Home $JAVA_HOME/bin/java
This technique has been tested successfully in two environments:
- Clean Snow Leopard install – Defaults to Java 1.6 – with manual install of Java 1.5 to run Java 1.5
- Existing Leopard – default Java 1.5 installation – with manual install of Java 1.6 – to run Java 1.6
