Home > Articles > Programming > Java

Java Perspective: Cocoa-Java Bridge

  • Print
  • + Share This
What if you could combine Cocoa (that easy-to-use extension to C that is the primary language used for development on the OS X platform) and Java (one of the most widely used languages on the Internet) to create an OS X native application that utilizes the power of Java's libraries? Marcus Zarra does just that in this latest article in his series on Cocoa from the Java developer's perspective.
Like this article? We recommend

In the early years of OS X, Steve Jobs promised that Java would be a first-class citizen of Apple's new operating system. Although the implementation of that promise is up to great debate, the developers at Apple did implement a bridge between Cocoa and Java.

This bridge allows for Java objects to utilize Cocoa objects, and Cocoa objects to do likewise with Java objects. Unfortunately, there are quite a few features in Cocoa that have not been implemented in the bridge—specifically, the newest features that are now available in Tiger. It also appears that Apple is not planning on adding these features to the bridge. However, that still does not affect the basic function of the bridge nor does it decrease its usefulness.

Quite a few developers who have used the bridge have used it to put a Cocoa GUI on a Java application. In fact, Interface Builder and XCode assist you so you can use Interface Builder to design your GUI and use Java in XCode to write the functionality for that GUI. However, there is another very powerful use for the Cocoa-Java Bridge: to utilize the vast sea of Java libraries that are available at little no or cost.

Although most of the Java libraries could be replaced by C or C++ libraries, they are often either archaic and/or difficult to use. As a Java developer, most of the Java libraries out there are second nature. Therefore if you want your Java application to connect to a SOAP service, talk to a J2EE application—it is simple to write the connectivity code in Java and then access it over the bridge from Cocoa and avoid having to deal with the difficult to use C and C++ libraries. Naturally if you are more accustomed to C or C++ over Java, this would not apply. However, Java has excellent network libraries and it would be silly to not use it for where its strength lies.

To demonstrate how easy it is to integrate a Java "back-end" into your Cocoa application, I will outline a simple GUI application that will retrieve a web page and display its source code in an NSTextView. Note that this is merely an introduction to the Cocoa-Java bridge and does not encompass its full rang of functionality.

A Simple Example

The first step is to set up the XCode project. In this example, the application will have only a single window, so I used a basic Cocoa Application template for the project. Then I added one Objective-C class named AppDelegate. The header is as follows:

#import <Cocoa/Cocoa.h>
#import "URLRetriever.h"

@interface AppDelegate : NSObject {
   IBOutlet NSTextField *urlField;
   IBOutlet NSWindow *window;
   
   URLRetriever *javaObject;
   NSString *source;
}

- (NSString *)source;

- (IBAction)retrieveAction:(id)sender;

@end

Open the MainMenu.nib file in Interface Builder and drag the header file into Interface Builder. In Interface Builder, instantiate the AppDelegate class. Next, Ctrl-drag from the file owner to the AppDelegate instance and connect it as the file owner's delegate. As can be seen in the following figure, the GUI itself is very simple, consisting of a NSTextView for the output, a NSTextField for the url and a NSButton to make it go.

After the GUI is laid out, connect the NSTextField to the IBOutlet in the AppDelegate and connect the NSButton to the IBAction method defined in the AppDelegate class. Finally, set up a binding for the NSTextView, connecting it to the AppDelegate's source method. This is all that is needed in the Interface Builder for this example, so it can be closed.

  • + Share This
  • 🔖 Save To Your Account