Xcode: Accessing Java from Objective C

Mar 26, '04 09:55:00AM

Contributed by: Morgoth

Getting and using Java objects in Objective-C Cocoa projects is easy ... once you figure it out. Unfortunately, Apple's documentation on this topic is not only scarce, it is also inaccurate. The goal of my hint is to provide you with the ability to develop a project including your own Objective-C code, Java library classes, and your own Java classes.

To facilitate your ability to see how this works in practice, my explanation I will use a Foundation tool. Note: I have verified that the same steps work for any Objective-C project type

Read the rest of the hint for the step-by-step explanation...

  1. Create a new Foundation project called "HelloBridge"
  2. Create a new Pure Java Package target called "JavaClasses"
  3. Create a new Java class called "HelloBridge.java" an add it to target "JavaClasses":
    
    public class HelloBridge {
       private String string = "Hello";
    
       public void setString(String string) {
          this.string = string;
       }
    
       public String getString() {
          return this.string;
       }
    
       public void printString() {
          System.out.println(this.string);
       }
    }
    
  4. Add "HelloBridge.java" to the "Sources" build phase in target "JavaClasses"
  5. Create a new empty file called "JavaInterfaces.h" and add it to target "HelloBridge":
    
    // Provide Objective-C interfaces for the Java classes
    // Not only good practice, it provides Code Sense
    @interface java_util_Vector : NSObject
    {}
    - (void)add:(id)anObject;
    - (id)get:(int)index;
    @end
    
    @interface HelloBridge : NSObject
    {}
    - (void)setString:(NSString *)string;
    - (NSString *)getString;
    - (void)printString;
    @end
    
  6. Modify "main.m":
    
    #import <Foundation/Foundation.h>
    #import "JavaInterfaces.h"
    
    int main (int argc, const char * argv[]) {
       NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
       
       // Load the Java VM
       id vm = NSJavaSetupVirtualMachine();
       
       // Start the Java class loader
       NSJavaClassesFromPath(nil, nil, YES, &vm);
       
       // Load a new instance of the java.util.Vector Java class into an Objective-C pointer
       java_util_Vector * vector = NSJavaObjectNamedInPath(@"java.util.Vector", nil);
       [vector add:@"one item!"];
       NSLog(@"item 1=%@",[vector get:0]);
       [vector release];
       
       // Load a new instance of our custom HelloBridge Java class into an Objective-C pointer
       HelloBridge * hello = NSJavaObjectNamedInPath(@"HelloBridge", nil);
       NSLog(@"item 1=%@",[hello getString]);
       [hello setString:@"Test"];
       NSLog(@"item 1=%@",[hello getString]);
       [hello printString];
       [hello release];
       
       [pool release];
       return 0;
    }
    
  7. Select "JavaClasses" from the targets drop box and Build it
  8. Select "HelloBridge" from the targets drop box and Build/Run it
  9. Gloat to all of your friends about how awesome OS X is for development
[robg adds: I have not tested this one, as it's well beyond my area of expertise.]

Comments (7)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20040321163154226