Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Sharing Objects Java <=> Objective-C

Sharing Objects Java <=> Objective-C
Thread Tools
Mac Elite
Join Date: Sep 2000
Location: Edmond, OK USA
Status: Offline
Reply With Quote
Jan 6, 2004, 02:44 AM
 
I know it's possible but I can't find any way to use a Java library from within Objective-C. For example, for all I can tell Acquisition is written in Objective-C, but utilizes many network components that are written in Java. How is this accomplished?

Say I have these classes in Java:

Code:
public class Manager { public Bean getBean() { return new BeanImpl(); } public static Manager getInstance() { return new Manager(); } } public interface Bean { // . . . void x(); } public class BeanImpl implements Bean { // . . . public void x() { // . . . } }
How would I go about doing this in Objective-C:

Code:
// This is ObjC pseudo-code Manager man = Manager.getInstance(); Bean bean = man.getBean(); bean.x();
This must be possible, but Apple doesn't have any documentation that I can find. Also, what is the overhead of this? Is there a load of boxing or marshalling going on?

And is the reverse possible? Can I send an ObjC object into Java and call methods on it?

I would like to avoid JNI if at all possible.

This is on Panther and I have all the developer tools installed, in case the info is there.
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jan 6, 2004, 04:44 AM
 
Courtesy of Cocoa Dev Central: The Java Bridge
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Mac Elite
Join Date: Sep 2000
Location: Edmond, OK USA
Status: Offline
Reply With Quote
Jan 6, 2004, 09:38 AM
 
Originally posted by Chuckit:
Courtesy of Cocoa Dev Central: The Java Bridge
Thank you very much. I had never seen that site before, very useful.
     
Mac Elite
Join Date: Oct 2000
Location: Edinburgh, Scotland
Status: Offline
Reply With Quote
May 26, 2005, 07:48 PM
 
Firstly, I apologise for dredging up such an old thread.

Secondly, I'm trying to do the reverse of what's in Cocoa Dev Central's web page: I need to access an Objective-C object from within my Java code. I've got a cocoa-java project and I need to call various CFM routines which can only be done via Obj-C if I've understood correctly (this bit works correctly in its own mini obj-c project).

What I'm trying as a test is the following:
Code:
testClass.m @implementation testClass -(void) testMethod { NSLog(@"test line printed\n"); } @end
In my Java code, I want to create an instance of testClass and call testMethod. I was under the impression that the Java-ObjC bridge would allow me just to do
Code:
testClass myTestClass = new testClass();
but it moans at compile time saying "testClass" can't be found/resolved.

I'd be really grateful for any pointers as my project's presently sitting in front of a bloody big brick wall! Thanks.
ClamXav - the free virus scanner for Mac OS X | Geobunny learns to fly
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
May 27, 2005, 12:49 AM
 
Firstly, in both Java and Objective-C, class names should start with a capital letter.

Second, I believe this thread on CocoaDev covers the issue pretty well (though I'm not sure you'll really love the answers).
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
May 27, 2005, 08:55 AM
 
Use JNI.
     
Mac Elite
Join Date: Oct 2000
Location: Edinburgh, Scotland
Status: Offline
Reply With Quote
May 27, 2005, 09:46 AM
 
Originally Posted by Angus_D
Use JNI.
Care to elaborate? Please?
ClamXav - the free virus scanner for Mac OS X | Geobunny learns to fly
     
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
May 27, 2005, 10:33 AM
 
     
Fresh-Faced Recruit
Join Date: Jun 2005
Status: Offline
Reply With Quote
Jun 26, 2005, 08:27 PM
 
Java can't see the obj-c methods but you can talk directly to your Obj-C objects by using NSKeyValueCoding

public void someJavaMethod(NSObject objcObject) {
objcObject.valueForKey("testMethod");
}

if you want to return a value you use valueForKey(), if you want to pass a value you use takeValueForKey() and do the following:

in Obj-C change the method to

- (void)setTestMethod: (NSString*)msg
{
NSLog(msg);
}

(note that the 'set' prefix is nessesary as takeValueForKey() looks for a setter method)

then in java call it:

public void someJavaMethod(NSObject objcObject) {
objcObject.takeValueForKey("this is a test message from java!", "testMethod");
}

Obviously this can be a bit cumbersome but if all you wanna do is call some of your existing code it can save some time.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:48 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2