Getting Started with Cocoa Java
April 09 2005 05:38 PM

I started out with the intention of building a Cocoa application that used the Xerces java libraries. Seemed like a straightforward problem yet it took me most of the day to get it figured out. Seems Apple's developer documentation doesn't quite cover this scenario and the hits from google were helpful but none contained the complete answer.

In summary, you need to follow these steps to add a third party JAR file to your Cocoa application:

  1. Start a new Cocoa Application in XCode. Double click MainMenu.nib. In Interface Builder create a subclass of NSObject called AppController. "Instantiate" it and "Create Files" for it (if none of this is familiar you'll need to read a good intro book on Cocoa programming).
  2. Back in Xcode add an awakeFromNib method to AppController.m
  3. In the menu, Select "Project > Add to Project...". Find your jar files, click "Add". In the next sheet click the "Copy items into..." checkbox. Click "Add" again. For this example I used xml-apis.jar and xercesImpl.jar from the Xerces2 Java distribution.
  4. Add code to your awakeFromNib: method
    - (void)awakeFromNib:
    {
    	id loader = [[NSClassFromString(@"org.apache.xerces.impl.xs.XMLSchemaLoader") alloc] init];
    	id model = [loader loadURI:@"/Users/emh/Desktop/entry.xsd"];
        
    	id componentMap = [model getComponents:2];
    	
    	int i;
    	for (i = 0; i < [componentMap getLength]; i++) {
    		id object = [componentMap item:i];
    		
    		NSLog(@"%d : %@", i, [object getName]);
    	}
    }
    
  5. In the "Groups & Files" panel click the triangle beside "Targets" and then the one beside your project name. Right click your project name and select "Add > New Build Phase... > New Copy Files Build Phase" from the context menu. In the Info sheet that pops up select "Java Resources" for your destination. Now click the triangle beside "Frameworks & Libraries", you should see your jar files there. Select them and drag them into the new "Copy Files" build phase.
  6. Last step is to edit the Info.plist file to contain the following (just add it to the end just before the </dict></plist>)
    <key>NSJavaNeeded</key>
    <string>YES</string>
    <key>Java</key>
    <dict>
    	<key>JVMVersion</key>
    	<string>1.4+</string>
    </dict>
    <key>NSJavaPath</key>
    <array>
    	<string>xercesImpl.jar</string>
    	<string>xml-apis.jar</string>
    </array>
    <key>NSJavaRoot</key>
    <string>Contents/Resources/Java</string>
    
  7. Build and run your application.

Some things I still don't understand:

  • Why does XCode put the jar files in the "Frameworks & Libraries" build phase? That seems to generate warnings about unknown library types and doesn't help the application find the Java classes any.
  • If you create a Cocoa-Java Application you are presented with a graphical Info.plist editor. Why not the same for Cocoa apps? To get the required edits for Info.plist I had to create a Cocoa-Java app and copy and paste them from the generated Info.plist.
  • What are these warnings that appear in my console for the Cocoa app but not for the Cocoa-Java app:
    Java HotSpot(TM) Client VM warning: Attempt to protect stack guard pages failed.
    Java HotSpot(TM) Client VM warning: Attempt to deallocate stack guard pages failed.
    

In spite of the questions I still have I hope this saves some time for others new mixing Java into their Cocoa.

Comments (0), Add Comment