A Bundle of Java
Volume Number: 15
Issue Number: 8
Column Tag: JavaTech
A Bundle of Java
by Danny Swarzman, Stow Lake Software
Developing a stand-alone Macintosh application in
Java
Preface
A typical Macintosh application allows the user to create a document, a file that
contains data structured in a way that is specific to the application. The application has
its own icon that appears in the Finder and an icon for each type of data the application
can create or edit. A user double-clicks the application icon to launch the app.
In contrast, a Java application is typically invoked from a command line in the shell.
Command line and shell are foreign expressions to most Macintosh users. They reflect
Java's Unix lineage. On non-Macintosh systems, the command line is where you type in
system commands, and the shell interprets and executes those commands.
Apple provides what you need to bridge the cultural gap. It's called JBindery and is
included in the Macintosh Runtime for Java Software Development Kit (MRJ SDK). You
can get the MRJ SDK and the latest version of MRJ from the Apple website. (The web
address is provided in the References section at the end of this article.)
Using the goodies in the aforementioned SDK, you can create a full-fledged Macintosh
application without writing a single line of C++ or Pascal. This article traces the
steps to create a sample application.
The sample application handles multiple document windows and supports the usual File
commands.
There are some features that would normally be part of a Macintosh application that
are not included in this sample application. A discussion of these exercises for the
reader is included near the end of this article.
The process for developing this type of application is:
• Create the sources.
• Create the resources.
• Compile Java classes into a .jar (Java Archive) file.
• Use JBindery to wrap the resources and .jar into an application.
If you use CodeWarrior Pro 4 or Pro 5, the last two steps can be combined. This
article will show the process using CodeWarrior Pro 4, and a simple tic-tac-toe
application. The source code for our Tic-Tac-App can be downloaded from
. It will also show what you need to do to bind the
application when you use another compiler.
What is a Java Application?
A collection of Java classes is an application if it contains a class with a method defined
as follows:
public static void main ( String args[] )
In operating systems that have a command line shell, the args parameter contains the
arguments from the command line that was used to invoke the program.
There are three standard file streams available to the Java application: System.in,
System.out, and System.err. Traditionally, these are assigned to a text terminal
(console window) or to files.
There are Java classes for interaction with a user in an window-oriented graphic user
interface. The basic classes are in the Abstract Windowing Toolkit (AWT).
There is also an extensive library of classes available for building a user interface in
Java, called Swing (part of the Java Foundation Classes). Swing differs from the AWT
in that its classes do not rely on platform-dependent peers for display. Instead, the
Swing classes are pure Java, and can take on a variety of appearances. Since the user
interface in the sample project is so simple, no Swing classes are used, only AWT.
What is a Macintosh Application?
Document Model
In a typical Macintosh application, the user interacts with a data structure through a
window. The user may store and retrieve data through the File menu in the application
or through the Finder.
In order for this to occur, the developer must create bundle resources and write code
to create the menus and handle the events. Class libraries, such as PowerPlant, take
care of most of the messy coding details.
Resource Fork and Bundle Resources
A Macintosh application, like all Macintosh files, contains a data fork and a resource
fork. The data fork (for PowerPC native apps) contains the object code. Among other
things, the resource fork contains bundle resources. These provide the Finder with the
information it needs to associate the application and its documents with icons in the
Finder.
Creator and Type
Each file is identified by two four-character designations. These are not stored as
strings. They are four characters packed into a 32-bit value.
• The creator code, or signature, in a document associates the document
with an application that has the same creator code.
• The file type identifies the structure of the data. An application may be
able to handle one or several file types.
The file type 'APPL' designates the file as an application. When the user opens a
document with a given creator code, the Finder will locate the application with same
creator code. It will open the application and send it an event to open the document.
When the application creates document files, it must specify the signature and file
type to correspond to its bundle resources.
How is a Java Application Run on the Mac?
In order for a Java program to run on the Macintosh, some Macintosh application must
instantiate the Java Virtual Machine and run the Java code in the instance of the Java
Virtual Machine. In order to do that, the application calls upon Macintosh Runtime for
Java (MRJ). MRJ is part of the system software starting in OS 8.1. It is supplied as a
code fragment, placed in the System Folder by the MRJ installer. New versions of MRJ
come out frequently and are available on Apple's website.