October 31, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Switching to the Mac for the Java Developer, Part 1

  • December 18, 2002
  • By Benoît Marchal
  • Send Email »
  • More Articles »

You might have been tempted by Apple ads on switching to Macintosh (www.apple.com/switch/ads). I was tempted enough to add two Macs to my network. I bought them primarily for digital photography, but I was also interested in the new, UNIX-based, Mac OS X.

In this series of articles, I will explore porting Java applications to Mac OS X. I assume you already are familiar with Java, Swing, and AWT, so I will concentrate on the Mac OS X aspects only. The series will be useful to Java developers who want to write more portable applications.

Why Java?

I chose Java as my primary programming language on Mac OS X for three reasons:

  1. It is portable.
  2. Mac OS X has excellent support for Java.
  3. I already know Java.

Portability is important because I own both Macs and PCs. Furthermore, most of my customers have Windows and Linux boxes. Java is the most sensible choice in a mixed environment.

Java is an excellent language for Mac development. Apple provides four main APIs for Mac OS X:

  • Cocoa, which is unique to Mac OS X
  • Carbon, which simplifies porting existing Mac applications
  • UNIX through a subset of POSIX
  • Java. Unlike other platforms, Java is not a layer on top of the operating system, but one of the primary APIs.

Finally, learning a new language and a new API is not necessarily difficult but it takes time. It makes more sense to use a language (Java) and APIs (AWT/Swing) with which I am already familiar.

User Interface Innovation and Java

Apple's old slogan was "Think different" and that certainly applies to Aqua, the user interface for Mac OS X. Although Aqua uses menus, dialogs, and windows, it differs significantly from the more common Windows or KDE/Gnome interfaces. Unfortunately, AWT and Swing were designed for those more common interfaces and are severely underpowered with Aqua.

In practice, pure Java applications under Mac OS X behave like crude Windows ports. One can take advantage of various Apple extensions to smoothen the user experience. We will study those extensions in this series. In this article, we will start by packaging a Java application for Mac OS X.

Application Package

One of the most visible differences between Aqua and Windows/KDE/Gnome is the absence of a Start menu. Aqua does not need one. The Start menu exists because what the user thinks of as an application is often a directory with several files (binaries, shared libraries, resources, JARs, and more). If one file is missing, it does not work. The Start menu associates an icon to the directory.

The Mac proposes a more intuitive solution called application bundles, which really are clickable directories. On the file system, an application bundle is a directory (with the .app extension). It contains the application files (binaries, shared libraries, resources, JARs, and more) and has its own icon. When the user double-clicks the bundle, it launches the application.

Application bundles are more intuitive if only because copying or moving the bundle copies or moves the entire application directory. No more invalid shortcuts and, in most cases, you don't need an installer. The user simply copies the bundle (the application) to his or her hard disk.

Bundles for Java Applications

The least you can do to make a well-behaved Mac application from an existing Java application is to create an application bundle. You will need MRJAppBuilder, a utility that should ship with your Mac (look for a "Developer Tools" CD or a "Developer Tools" directory). Alternatively, you can download it from developer.apple.com.

MRJAppBuilder proposes a dialog box with four tabs, as illustrated in Figure 1. On the first tab, you specify the application's main class, its classpath, the resulting application bundle, and its icon (click on the icon in the "output file" section to choose the icon).

Entries in the classpath are relative to the application bundle. You can add JAR files through the "Merge Files" tab. The classpath will be of the form Content/Resources/Java/myjar.jar.

On the Mac OS tab, you want to enter the application name in the CFBundleName property.

Finally, in the Merge Files tab, list the application files, including JAR files and other resources (images, HTML, and more). These files are copied in the Content/Resources/Java directory in the application bundle. Click the "Build Application" button to create the bundle.

Figure 1

Next Steps

Congratulations! You have taken the first step to distribute Java applications on the Mac. In the next articles, we will see how to improve the application so it integrates more nicely with Aqua.

Benoît Marchal is a Belgian developer. He is the author of XML by Example and other XML books. He was originally attracted to the Mac for digital photography but he has learned to enjoy the robust Java implementation on Mac OS X.






Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel