MacDevCenter    
 Published on MacDevCenter (http://www.macdevcenter.com/)
 See this if you're having trouble printing code examples


Mixing Java and Titanium: Part 1

by James Duncan Davidson
09/04/2001

Editor's note: Recently James Duncan Davidson published a series of weblogs on the subject of Java and Mac OS X for O'Reilly Network. These blogs contained lots of useful information that was too good to get buried deep within the network labyrinth, so we've updated them and are now presenting the content in a series of three articles. This is part one of the series.

At this year's O'Reilly Conference on Java in Santa Clara, I unwittingly became the guy with the coolest toy around. Whenever I pulled out my new Titanium PowerBook to get some work done, people started coming by to ask questions about it. The shiny titanium shell must have had something to do with that. However, as soon as they saw Mac OS X, curiosity really peaked. And as I gave demos showing the BSD underpinnings of Mac OS X, and the excellent Java virtual machine implementation, they started talking about getting one. Needless to say, I didn't actually get much work done.

So how did I, a self-confessed Unix weenie and sometimes Windows user end up being an unofficial Mac evangelist? Well, I've been looking for years for a platform that combines a good, fast Java virtual machine, a Unix command line, and the ability to keep up with the several thousand e-mail messages per day that I receive. And I wanted a platform that looked good to boot. I should mention that there has been a Mac around the house for my digital photography habit over the last few years, but it hasn't been something that I have used for serious work.

Enter Mac OS X, a brand new operating system that takes the best of the Mach kernel, BSD, JDK, and Macintosh technology and combines them into one killer platform. As soon as Fred Sanchez, who used to work in the Core OS Group at Apple, introduced me to it several months before public beta, I was intrigued. Now that it has been shipped with JDK 1.3 and Hotspot, I've taken the plunge and moved to using a Titanium G4 full time. And it has been treating me very well. To be sure, there are a few rough edges, but overall I'm really impressed.

Photo.
The Titanium PowerBook drew lots of attention at the O'Reilly Enterprise Java Conference. (Photo by D. Story/O'Reilly Network)

Comment on this articleWhen we first heard that Mac OS X would be an excellent platform for Java development, we smiled. But the fact of the matter is that Apple has really done its homework here. Let's talk about Java development on Mac OS X.
Post your comments

Back at the conference: The folks at O'Reilly noticed the number of people that were interested in this new environment (not to mention that I think a few of them want a Titanium as well) and invited me to share my experiences with Java and Mac OS X -- to give you a peek over my shoulder as I play around with this new system. So, read on and I'll do my best to give an honest hacker's viewpoint on mixing Java and Titanium.


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 java.sun.com 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:

/System/Library/Frameworks/JavaVM.framework/Versions/1.3/Home

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:

/System/Library/Frameworks/JavaVM.framework/Home

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.

Copyright © 2009 O'Reilly Media, Inc.