views:

264

answers:

3

Hello,

I can't find a decent Cocoa Charting Library so I would like to use JFreechart.

Is there any way to call a Java program from a Cocoa Application? I was thinking in writing a java application that returns an image of the chart and I would display it inside my Cocoa app.

Thank you,

+4  A: 

As far as I know you have two options:

  1. Use Java Native Interfaces (JNI) and call your Java classes from C (Cocoa).
  2. Launch a separate java process within your COCOA application to create graphics for you. You can use NSTask for that.

In my opinion, even though first option is tougher to implement, it's more elegant and probably faster. But second option will definitely take less time to implement.

Hope it helps.

Pablo Santa Cruz
+4  A: 

Apple has a very good (but somewhat old) sample code project of how to create an app that uses JNI in Xcode:

http://developer.apple.com/java/jniuniversal.html

Be sure to check out the sample code associated with that:

http://developer.apple.com/samplecode/MyFirstJNIProject/MyFirstJNIProject.html

Also this Tech Q&A:

http://developer.apple.com/mac/library/qa/qa2005/qa1295.html

I used that example to successfully create an app which uses JNI to launch and communicate with a single JVM instance at runtime (rather than spawning lots of NSTasks as Pablo mentioned in #2 below). Basically, i took the approach of Pablo's 1st suggestion below with this app:

http://ditchnet.org/xslpalette

Todd Ditchendorf
I'll look into using JNI, thank you for those links.
simao
Sure thing. :) One word of advice, if I may... doing this sort of thing is a real pain in the arse. I have not done any research on native charting tools in C/C++/ObjC, but if you can find a native one, it might be better to invest the time in learning the native tool rather than investing the time in JNI/talking to Java. Might save you time/headaches. But again, I haven't looked, so I'm not really sure. :|
Todd Ditchendorf
+4  A: 

You may want to look at the Core Plot framework, a Cocoa-native plotting framework for Mac and iPhone. It's relatively new--not as mature as JFreeChart--but it may do what you need. It would certainly save you the pain of JNI or inter-process hackery.

Barry Wark
I did look at Core-Plot but they don't offer any documentation. Oh well, maybe its easier than using JNI anyway... Thank you.
simao
Documentation is definitely sparse on the Core Plot website. The xcode project has a Doxygen build target, however, that produces API documentation and a sample app that demonstrates most (or maybe all) of the framework functionality so you can at least look at example code.
Barry Wark