Home > Articles > Programming > Java

Java Perspective: Cocoa-Java Bridge

Marcus Zarra
  • PrintPrint
  • Share ThisShare This
  • DiscussDiscuss
Close WindowMarcus Zarra

Marcus Zarra

Learn more…

Basic Animations with Core Animation
Dec 28, 2009
Marcus Zarra and Matt Long on Core Animation
Jun 5, 2009
Introduction to Migration in Apple's Core Data
Mar 7, 2008
A Look at Apple's Core Animation
Feb 1, 2008
The State of Java Development on OS X
Jan 4, 2008
Packaging Your Application for OS X
Nov 21, 2007
Tiger vs. Leopard: Which Should You Develop For?
Oct 26, 2007
Building on Apple Sync Services
Sep 8, 2006
Getting in Sync with OS X
Sep 1, 2006
Syncing Your Data the Cocoa Way
Aug 11, 2006
Controlling Both Ends of the Communications Channel: From Cocoa to Servlets
Jun 2, 2006
Cocoa: Working with XML
May 19, 2006
Java EJB 3.0: A Hibernate Clone?
Apr 28, 2006
Document Level Parameters Using Core Data: A Guide for Cocoa Developers
Apr 7, 2006
Tips for J2EE Development
Feb 24, 2006
The Cross-Platform Java GUI: Designing Code for More Than One Operating System
Feb 10, 2006
Avoiding Java Exception Abuse
Dec 22, 2005
Java Perspective: Advanced Core Data
Dec 16, 2005
Java Perspective: Cocoa-Java Bridge
Dec 1, 2005
Java Perspective: Core Data and the Mac OS X Developer
Nov 23, 2005
Java Perspective: Key-Value Protocols, Behind the Magic of Mac OS X Development
Nov 11, 2005
Java Perspective: Cocoa Subclasses and Delegates
Nov 4, 2005
The Java Perspective: Cocoa's Interface Builder
Oct 21, 2005
Maven: Handling Multiprojects
Sep 23, 2005
Java, Maven, and Your Web Development Project
Aug 26, 2005
Ant User, Let Me Introduce Maven
Jul 29, 2005
Using JAAS Authentication with JBoss
Jun 17, 2005
Building a Custom JBoss Login Module
Jun 10, 2005
Testing Java Enterprise Applications with Cactus
May 20, 2005
Ant: Building a Better Build File
May 6, 2005
XDoclet: Entity Bean Relationships
Apr 8, 2005
The Java Developers' Introduction to XDoclet
Jan 7, 2005
An Introduction to Cactus
Dec 30, 2004
Using Multiple Databases with JBoss
Nov 24, 2004
JBoss Application Configuration Breakdown
Nov 5, 2004
Sortable Swing JTable
Oct 1, 2004
Building a Professional Swing JTable
Sep 10, 2004
Creating a Custom Java Swing Tablemodel
Aug 27, 2004
Adding Workflow Control to Your Java Applications
Mar 26, 2004
The Mac Developer Network
By on November 21, 2007 No Comments
Steve Scott of Late Night Cocoa has started a new podcast targeted specifically at Macintosh Developers.  One of the first episodes on this podcast is a roundtable of developers with several very well known developers starting with Daniel Jalkut, Uli Kusterer and Fraser Spiers along with myself.
Once more unto the [Beta Cycle], dear friends, once more
By on November 16, 2007 No Comments
Today marks the first semi-public release of iWeb Buddy, a new application that I have been developing for the past few months.  Unlike seSales or applications developed as a consultant, this development cycle has been a blur of activity.
Apple -- You Did Me A Disservice
By on October 26, 20073 Comments
Today is the day that most Mac users have been looking forward to for over a year.  Ever since Steve Jobs got up on stage last August and showed us Leopard for the first time.  Ever since that first peak almost all Mac users and I dare say ALL Mac developers have been looking forward to this release.  Unfortunately, today is also a day that Mac developers are going to be scrambling like rats on a burning ship.  This is because Apple decide to thumb their nose at us.
Further development of Bullfrog 2 for OS X
By on October 11, 2007 No Comments
Things are starting to speed up on the development of Bullfrog 2 previously mentioned here.  In our continuing disclosure about the development process, Jon Trainer has written a very informative article on how the actors are configured.
Starting over is hard to do
By on September 18, 2007 No Comments
I mentioned before that I am working with Jon Trainer of Outer Level to write Bullfrog 2 for OS X.  This development effort will be targeted at the as of yet unreleased OS X 10.5 Leopard.  We also promised to blog about the development as it moved forward.  Unfortunately not everything has been roses.
Find the Bug: A Book of Incorrect Programs

Like this article? We recommend
Find the Bug: A Book of Incorrect Programs

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.

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 ThisShare This
  • Your Account

Discussions

Future of the Bridge?
Posted Jan 28, 2008 01:02 PM by morph38927
0 Replies

Make a New Comment

You must log in in order to post a comment.

Related Resources

Jennifer  BortelWin FREE iPhone Developer Books and Videos- Introducing @InformIT Giveaways
By Jennifer Bortel on February 5, 2010 No Comments

Apples’s recent iPad announcement made our hearts flutter so we couldn’t resist making an announcement of our own!

Today marks the first ever @InformIT Giveaway!

We’ll regularly post a video like this one profiling spectacular prizes we’re giving away—from books and videos to T-shirts and other exciting stuff. Check out the video below to see the giveaways for today, and then scroll down for more prize details and instructions on how to win them!

So Far So Good
By John Traenkenschuh on February 2, 2010 No Comments

So far, Win 7 is making a thoroughbred of what has been a plough mule laptop

Dustin Sullivan"Every OSX developer should have this book on their desk."
By Dustin Sullivan on February 1, 2010 No Comments

That was the sentence Mike Riley ended his recent Dr Dobb's CodeTalk review of Cocoa Programming Developer's Handbook with.

See All Related Blogs

Informit Network