Mac OS X & Java: Where's my $JAVA_HOME?
by James Duncan Davidson
The first thing I needed to do when starting to work with OS X's Java implementation was set my JAVA_HOME environment variable. Several Java programs use this environment variable to help them locate various JAR files that are not on the default classpath. Since the default Terminal shell on OS X is tcsh, it is easy enough to set your JAVA_HOME to the necessary string as long as you know what that string should be. However, finding the installation directory of Java on OS X was an adventure of its own that led to some interesting questions.
To find the right value that JAVA_HOME should be set to, I first executed a `which java` in the nearest Terminal. this told me that /usr/bin/java was the executable that launched the VM. A quick `ls -l /usr/bin/java` told me that it wasn't a symbolic link to the JDK installation that I was hoping for. Instead, it is a short shell script that executes:
exec /System/Library/Frameworks/JavaVM.framework/Versions/1.3/Commands/java "$@"
From this bit of information I was able to poke around and find that I could set JAVA_HOME to:
This was all I needed to keep everything happy. But the "Versions/1.3" string intrigued me a little bit, so I dug a bit deeper into the directory structure of the framework and actually found that I could us a shorter string:
To solve this problem forevermore, I dropped the following line into my ~/.login file:
setenv JAVA_HOME /System/Library/Frameworks/JavaVM.framework/Home
Now that I had a solution to the problem at hand, I really wanted to know more about how frameworks were implemented under OS X. After all, everything from AppKit to Cocoa and Carbon to OpenGL is contained in the /System/Library/Frameworks folder. I found the answer in Apple's "Inside Mac OS X: System Overview". In short, a framework is a type of bundle that packages shared libraries along with resources. In addition, frameworks are versioned which should help developers address compatibility problems as OS X evolves.
After wrapping my head around frameworks for a while, I dug into the other kinds of bundles that exist in OS X, including application bundles. It turns out that items that look like single file applications in the Finder are actually hierarchically structured directories containing the program and its resources. I'll talk more about how you can package up Java applications into these bundles in a future weblog.