Java back with Cocoa front?

Discussion in 'Mac Programming' started by Forquare1, Oct 29, 2009.

  1. Forquare1 macrumors newbie

    Jun 22, 2008
    Hello all,

    Although I've done some Objective-C programming, I'm far more confident in coding in Java.
    Is it possible for me to create a GUI using IB, writing the controller in Objective-C, and then linking the controller with a Java model?

    I'm planning to code primarily for Snow Leopard (as that is my active platform). I will then look at support on Leopard for potential users who haven't upgraded.

    If it is possible, how can I achieve this? Is there some documentation I should read involved in this?

    Many thanks,
  2. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX

    You can embed a JVM in your C (or in this case Objective-C) program, and make calls to your java code from C (and vice versa). If you wouldn't mind making the front-end Objective-C++, using JNI is much easier from C++ rather than straight C.


    EDIT: You will have to be careful not to mix your nomenclature too much. You might get confused about whether that nib is of a cocoa bean or coffee bean. Heaven forbid you want to mix some NUTMEG into this Mochachino you're brewing up.
  3. Forquare1 thread starter macrumors newbie

    Jun 22, 2008
    Many thanks Lee,

    I shall look into that over the weekend if time allows.

    Many thanks once again,

  4. Forquare1 thread starter macrumors newbie

    Jun 22, 2008
    I've been reading through the JNI book, and in the first example, I stumbled.

    It says to compile a shared library on solaris do this:
    cc -G -I/java/include -I/java/include/solaris HelloWorld.c -o
    However, I'm on OS X...How can I achieve the same thing? (In XCode or on the command line)

    Many thanks,
  5. wrldwzrd89 macrumors G5


    Jun 6, 2003
    Solon, OH
    The command line options are almost identical, but the folders where the the needed include files live are different. I don't think this can be done with the XCode GUI, at least in Snow Leopard, due to XCode's lack of Java support. I'd tell you where the include folders are, but I'm not at a Mac (let alone have access to Xcode). :eek:
  6. chown33 macrumors 604

    Aug 9, 2009
    Xcode 3.2 has Java project templates in its Organizer window. Open the Organizer window, click the + icon below the list, and see the available templates.

    Like many things in Xcode 3.2, it's not in the same place as before. For example, there used to be a control to get the detailed build log (aka build transcript). Here's the new way:

    There's a "JNI" section heading here:

    Also see:
  7. Forquare1 thread starter macrumors newbie

    Jun 22, 2008
    Cheers for that, I did wander where Java had disappears to...

    Oddly enough, there is a huge difference between XCode 3.2 and XCode 3.2.1, I have the two versions on my MacBook and Mac Pro and they are fairly different in layout...

    As to compiling a C file into a library, I'm still not sure, I've searched the web, but I'm obviously asking a stupid question or looking for the wrong thing...:(
  8. chown33 macrumors 604

    Aug 9, 2009
    Under the "JNI" section heading here:

    there are examples of actual command-line compilation of JNI library source. That's why I posted that URL before.

    If those examples don't work or don't meet your needs, be specific about how they don't work or don't meet your needs. That includes posting any error messages, rather than just saying "It doesn't work" or "It has an error".
  9. Forquare1 thread starter macrumors newbie

    Jun 22, 2008
    Aha! I'm sorry chown33, my internet is playing up and I didn't see your last two links before as only parts of the page loaded.
    You link help's me very much.

    Thanks you all for all your help :)
  10. Nordwolf macrumors newbie

    Oct 30, 2009
    Why not just do the whole application in Java + Swing if you want to use Java? The Swing look and feel in Apple's latest versions of Java is practically indistinguishable from a native Cocoa application. This would be a lot less error prone than trying to use JNI. And the Java-Cocoa bridge is deprecated, so I would not suggest using that either.

    Take a look at some of the latest screen shots of NetBeans on Mac for example to see how good the look and feel has gotten. Here's a link to one:
  11. ranguvar macrumors 6502

    Sep 18, 2009
    You might want to take a look at Ken Orr's blog. He creates custom controls / views using Swing that you can then implement in your Java app. If you stick to his widgets and Swing's native-looking components, your application will look virtually undistinguishable from a native Cocoa app.
  12. elppa macrumors 68040


    Nov 26, 2003
    One problem is the “unified toolbar” in NB looks like a unified toolbar but doesn't behave like unified toolbar. This is a retrograde step in my opinion because at least in previous versions the draggable area was clear.
  13. ranguvar macrumors 6502

    Sep 18, 2009
    Again, let me point you to Ken Orr's blog and his Mac Widgets.
    His unified toolbar looks & behaves exactly like a native unified toolbar.

Share This Page