oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Mixing Java and Titanium: Part 1
Pages: 1, 2

Mac OS X: Developer Appeal

What is it about Mac OS X that is so attractive? What is the substance underneath the sleek hardware and cool window effects?

First off, OS X is Unix underneath. It isn't just a "Unix-like" OS, nor is it an OS that exposes only some POSIX-like functionality to C programmers. At any time, you can pop open a Terminal window to examine processes, grep through files, or execute a quick Perl script. Or, better yet, pipe the results of "grepping" through a set of files to a Perl script.

Thankfully, Apple isn't ashamed of the Unix foundation of Mac OS X and hasn't tried to hide it from the power user. In many respects, I feel like I am running a FreeBSD operating system that lost X Windows along the way in favor of something much nicer -- a platform that can run the latest versions of Office and Dreamweaver without requiring me to dual-boot.

The latest and greatest Java Development Kit is already installed by default. For the first time, there's no need to surf over to to pick up the newest bits of code. Apple exceeded expectations with its implementation of the JDK. Not only are Java threads mapped to Mach kernel threads (which was expected), but each virtual machine instance cooperates with other running virtual machines to increase performance. It does this by caching loaded class files as well as HotSpot-compiled code in shared memory. If a class file has already been loaded and optimized by one virtual machine, another virtual machine can just use that code without having to perform the work again.

Finally, Mac OS X ships with a strong suite of development tools. Apple is the first vendor to ship a consumer operating system with a fully functional set of development tools in the same box -- for no extra charge. And not only are Project Builder and Interface Builder (Apple's excellent IDE tools) included, so are emacs, vi, and other Unix text editors. This is perfect for those who are comfortable using traditional tools, or who need to use something familiar when the pressure is on.

In essence, even though Mac OS X is strikingly new, it's also surprisingly comfortable for any developer -- Java or otherwise -- that has a Unix background. No matter how pretty the interface is or how cool it is that movies still play as their windows are being minimized, you can get right down to work and get things accomplished. If this is what "Think Different" means, I'm all for it.

Mac OS X and Java: Where's my '$JAVA_HOME'?

The first thing I needed to do when starting to work with Mac 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. Because 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 Mac OS X was an adventure of its own that led to some interesting questions.

To find the right value for JAVA_HOME, I first executed a `which java` in the nearest Terminal. this told me that /usr/bin/java was the executable that launched the virtual machine. A quick ls -l /usr/bin/java told me that this wasn't the 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 running. 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 use 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 Mac OS X evolves.

After wrapping my head around frameworks for a while, I dug into the other kinds of bundles that exist in Mac 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 the upcoming articles.

James Duncan Davidson is a freelance author, software developer, and consultant focusing on Mac OS X, Java, XML, and open source technologies. He currently resides in San Francisco, California.

Return to the Mac DevCenter.