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:
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.
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:
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.
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.