Setting CurrentJDK on Mac OS for Java6, VisualVM support. Tomcat and other services reference CurrentJDK for JAVA_HOME

I recently installed Java 6 on my Mac laptop, which includes VisualVM. Its a nice improvement over prior vm monitoring features in Java 4 & 5.

Testing the VisualVM behavior, I launched Tomcat 6 in Eclipse, but quickly found that Tomcat was still referencing Java 1.5. How was it doing that, I wondered? A little investigation revealed the answer, as well as info about how the Mac is configured for Java.

The Applications->Utilities->Java Preferences app allows you to set which JDK you prefer apps to use, but unfortunately this isn’t sufficient as some apps like Tomcat get the JDK via another avenue (/usr/libexec/java). After tracking it down, here is what I discovered.

open a shell and run: man java_home

you’ll get a one page description about how the Mac configures the JAVA_HOME environment variable, which is needed by many java applications. Apple does things in an unusual – but you might say very elegant – way, if only it was more clearly communicated.

type the following and hit enter:

/usr/libexec/java_home

you’ll get a path to a particular version of java, which is the default version of the JDK for the command line and shell scripts. What if you want that to point to a different version of the JDK? Well, it turns out that /usr/libexec/java_home gets the path from another path (/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK) which is a symbolic link to one of the installed versions of the JDK on the computer.

cd /System/Library/Frameworks/JavaVM.framework/Versions

the above path will show you all the installed versions of java on your machine as well as several symbolic links to the installed versions. The symbolic link that matters most, however, is this:

/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK

Whichever JDK the above link points to, is the JDK that /usr/libexec/java_home will ultimately refer to, and therefore any app (such as tomcat’s startup.sh script) that requests the java_home path will ultimately use.

ls -la

in the Versions/ folder and you’ll see where the symbolic links are pointing. In my case, after installing Java 1.6, CurrentJDK/ still pointed to 1.5/ (which is itself a link to 1.5.0). So I had to update the CurrentJDK/ symbolic link to point to the 1.6 JDK.

sudo ln -fhsv 1.6 CurrentJDK (from the Versions/ folder)

That made the change. I stopped and then restarted tomcat and it ran in the new JDK. Such a change is definitely worth making with VisualVM because it gives you monitoring functionality (such as running a manual GC or a Heap Dump) that you can’t get from 1.5 or earlier JDKs.

Advertisements

4 Responses to Setting CurrentJDK on Mac OS for Java6, VisualVM support. Tomcat and other services reference CurrentJDK for JAVA_HOME

  1. daniel says:

    I was scratching my head over this for longer than I’d care to admit. Thank you!

  2. solerous says:

    Wow, exactly what I was looking for. Thank you!

  3. pete says:

    Thanks, you saved me a ton of time!

  4. Jay says:

    Excellent pointer!! Thanks a ton!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: