views:

259

answers:

2

Hi guys, I want to create native Mac OS X application using Cocoa + Objective C but I need to connect to proprietary data source, and for this, owner of the data source only provides Java library. So I need to somehow import this Java library into my project and call functions on its Java classes.(Or create java wrapper around this library and then call my wrapper from objective-C).

Now, how can I do this? Quick google search leads me to JNI but I haven't found any good and actual(current) article/tutorial. I would really need some HOW TO article, how to load this java library, start VM if needed, and how to create java objects and call functions on them. Really something simple and I can move from there. Thanks.

Just to clarify, I repeat: I WANT to call Java functions from Objective-C, I do NOT want to call native functions from Java.

A: 

You're looking for the Java-Objective C bridge, try looking at this article or on Apple's developer site. Be aware it is deprecated, that is it isn't being kept up to date with changes to Cocoa. But if you're just using it for an API passing standard Java datatypes you should be OK.

Chris
+5  A: 

You're probably looking for the Invocation API, a little-known corner of JNI which allows you to load the Java runtime in-process.

That said, you might have an easier time of it with a Java service application that communicates with your Objective-C application over sockets.

Jeffrey Hantin
+1 for using separate processes. You will likely have a much easier time writing a service in Java that communicates with your data source and with your Objective-C app.
Barry Wark
Hi Jeffrey,yes I saw that Invocation API, I'll get it a closer look. This idea of java app and my cocoa app communicating over the sockets is quite good. But I would prefer to start this java app on background when my cocoa app is started not to run as service all the time, do you know if something like that is possible?(I used to be .net developer for 6 years, doing mac stuff only for 4 months now, so sorry if my questions are stupid)
Michal
There is no reason your Objective-C app could not start the Java app as an application whenever necessary (Like starting any other binary). You could then use sockets to communicate with it. You might even implement an "End" command to have it terminate itself when you are done, that way it's only running when you need it. You will have to delay a few seconds before you connect to it, java is pretty slow to start apps.
Bill K
There's an example of using the invocation API here: http://jove.git.sourceforge.net/git/gitweb.cgi?p=jove/jove;a=tree;f=native/comp/jvminvoke
nall