2.3 Additional APIs and Services

The Mac OS X Java installation includes several additional APIs and services besides the default JDK installations available from Sun for Unix and Windows. In theory, you could build applications for the Mac OS X Java platform that won't run on other platforms. Purists may argue that Apple's JVM is therefore no different from the infamous Microsoft JVM and its incompatibilities. It is very different, however.

Apple hasn't removed anything from their JVM implementation. All the expected services, including the politically contentious RMI, are available on the Mac OS X Java platform. If you build your application against normal J2SE APIs, you will have no problem porting your applications to other platforms. For more information on cross-platform compatibility, check out Chapter 9.

However, Apple provides many features in addition to the core Java APIs. Spelling, integration with QuickTime, and the Apple look and feel, for example, are specific to the Macintosh, and you will have trouble porting applications that use them to another platform unless you're willing to change some of your application's code. However, this book will help you recognize what is Mac-specific, and you'll soon avoid these APIs or adjust them for use on other platforms when cross-platform compatibility is a concern.

Later chapters cover many additional services, such as the Java Speech and Spelling Frameworks and application bundling. For now, though, here's a summary of what the Mac OS X JVM adds to the standard Java bundle:

  • Support for Aqua, the translucent, swooshing, pulsing user interface Apple introduced with Mac OS X (as well as Metal, the default Java look and feel)

  • External BSD tool support (significant if you need man, ifconfig, or other unusual BSD tools)

  • Java interfaces to QuickTime, for multimedia support

  • Java interfaces to OpenGL, for sophisticated 3D graphics

  • Java APIs for the Foundation and Application Kit frameworks in Cocoa

  • Some J2EE packages, including RMI over IIOP, JNDI, JDBC, and JSSE

  • Java threads implemented as native Mach threads, which provides free symmetric multiprocessing

  • Automatic sharing of Java class data and HotSpot-compiled code across instances, which dramatically reduces overhead for running multiple Java applications (reducing memory and increasing performance)

  • Automatic double-buffering of all windows

  • Hardware acceleration availability for Swing applications

  • MRJAppBuilder support, allowing you to create a seamless integration with the Mac OS X Finder GUI shell

  • Integrated support for XML-RPC and SOAP

If you're already a Java developer, most of your standard cross-platform development efforts will remain unchanged. If you're building Swing applications, you'll continue to develop them the way you always have. If you're building server-based applications (for example, with Apache Tomcat), you'll use the same commands and the same packages. The most significant changes will apply to configuring and installing, as well as deciding which Apple-specific features will be easy to add and will provide real value for your users and customers. You should be concerned about cross-platform techniques only when using Mac-specific features, like those in the list above.

However, many of these technologies are already cross-platform, despite their nonstandard functionality. For example, you might add QuickTime support for Java first on Mac OS X, and then decide to make that support available for your Windows users. The rule, then, is not to avoid these extensions like the plague, but to be aware of when you use them. Learning more about these features will help you decide when to use them.

2.3.1 Hardware Acceleration

One interesting feature of the Mac OS X Java implementation is its support for hardware acceleration. When you work with a release prior to JDK 1.4, you'll want to manually enable hardware acceleration. On more current versions of the Mac OS X JDK, though, this support is turned on by default.

2.3.2 Multiple Mouse Buttons

The hardware Apple ships includes only mice with a single button. This limitation is unfortunate, as the second mouse button has become an important GUI standard on most platforms.

On non-Mac platforms, icons often serve as "nouns," and right-clicking typically activates a pop-up menu that provides a list of "verbs" or actions that can be performed on that noun. In many ways, this approach is more intuitive than clicking an object to select it and then traveling to a distant menu bar to actually perform the action.

In any event, you may want to buy an inexpensive two-button USB mouse to use with your Mac OS X machine, especially if you plan to use the popular Java-based IDEs. Although two-button mice are not Mac standards, Mac OS X ships with support for two-button mice, as well as mice with scroll wheels.

If you are unwilling or unable to use a two-button mouse (for example, on your iBook or PowerBook when traveling), you can simulate the second mouse button by holding down the Control button while you click.

An application could be written for another platform that actually maps a Control-click with the single button to one action and clicking with the second button to a different action. This situation would be unusual, but you should be able to check the configuration options for that application to set different modifiers (and write a note to the application's developer asking for a more manageable control mechanism).

2.3.3 Java on Classic (MRJ)

Java isn't new to the Mac OS platform; the previous version, now dubbed Classic, included a Java Virtual Machine. To distinguish from other JVMs, the Classic Mac OS implementation was called the Mac OS Runtime for Java (MRJ). That JVM never progressed beyond support for JDK 1.1, even though the MRJ version number eventually crept up to 2.2.5. Because every Mac OS X machine includes Classic, each Mac OS X machine technically includes two JVMs: the JDK 1.3.1 (or later) version running native on Mac OS X and the JDK 1.1.8 JVM running in the Mac OS 9 Classic environment.

If you are new to the Mac OS X environment, Mac OS Classic might seem bizarre. Open the Apple Applet Runner, located in /Applications (Mac OS 9)/Apple Extras/Mac OS Runtime for Java/Apple Applet Runner.

If you have Mac OS 9 installed on another partition or hard drive, you will need to change to that volume or click on the appropriate hard drive icon in the Finder.

If Classic isn't already running, you will see a dialog noting that the Classic environment is starting. Click on the "Show Mac OS 9 desktop window" triangle, and you will see the old Mac OS 9 boot screen with a series of extensions marching across the bottom, as shown in Figure 2-2. Congratulations! You've just booted an entire alternate operating system as a process in Mac OS X.

Figure 2-2. Launching Classic

This environment is known as True Blue (TruBlueEnvironment in the process viewer). Eventually, the Classic Mac OS will finish booting and the Applet Viewer will appear in the Dock. Its appearance is the first sign of a set of interesting hacks that Apple has made to make the visual appearance of the two environments coexist more peacefully. Clicking on the Applet Viewer Dock icon, however, reveals a radically different menu bar (visually and in terms of the layout) from the standard Mac OS X menus.

From the Applet Viewer, click on the Applets menu, and select "Lightweight Gauge example1".

As shown in Figure 2-3, you'll see a series of shifting bars over the face of a colorful coworker (er, primate). When you click on a menu item in the Applet Runner, the bars stop moving; simply clicking a menu item blocks the environment.

Figure 2-3. Applet Viewer

The Mac OS X equivalent to the Apple Applet Runner is called Applet Launcher, and you can find it in /Applications/Utilities/Java. To run the Lightweight Gauge demo, as shown in Figure 2-4, you'll need to open the following URL (you can navigate to it using the "Open..." button):

file:///Applications (Mac OS 9)/Apple Extras/Mac OS Runtime For Java/Apple 
Applet Runner/Applets/Lightweight Gauge/example1.html
Figure 2-4. Mac OS X Applet Viewer

In comparison to the Classic MRJ, clicking on the menu bar in a native Mac OS X applet will show off both Mac OS X's slightly translucent windows and its multithreaded nature. The bars continue to move, and no blocking occurs.

You can also find an MRJ SDK at http://developer.apple.com/java/text/download.html. It includes a variety of tools, including a wrapper mechanism for launching command-line-based tools. MRJ has some interesting features, including a JAR caching mechanism for improving network performance of applets, which may be a required environment for legacy system support.

Ultimately, there is little reason to invest a lot of time and energy in the Mac OS Classic environment. It's a very difficult environment, since it runs an obsolete version of the JDK. Apple has clearly stated that the future direction of the Macintosh platform is Mac OS X, and unless you have significant development and testing resources, you probably shouldn't spend time or money on what even Apple considers a dead platform.