Java JNI for Mac question

Discussion in 'Mac Programming' started by LD220, Mar 9, 2010.

  1. LD220 macrumors member

    Jun 3, 2009
    Hi everyone. I'm attempting to learn a little bit about Java JNI and I wanted to run this example on my mac ( I got through the first few steps, writing the .java, generating the .h, and writing the .c, but after that I think the steps are Windows specific. Does anyone know how to compile and run this easy example in the terminal on a Mac? Thanks!

    Also, if you know of any other tutorials that would be helpful, I'd really appreciate suggestions.
  2. chown33 macrumors 604

    Aug 9, 2009
    If all you need are the C compiling commands:
    gcc -x c \
      -I/System/Library/Frameworks/JavaVM.framework/Headers \
      -c Foo.c -o Foo.o
    gcc -dynamiclib -o libFoo.jnilib Foo.o
    Replace "Foo" in the above with the name of your source or library, as appropriate.

    The output library MUST be named with a leading "lib" and a trailing ".jnilib" or ".dylib". The Java code to load "libFoo.jnilib" would be:
        System.loadLibrary( "Foo" );
        System.loadLibrary( "libFoo" );
    If you can't get it to work, post again and I'll upload a complete example (no explanation, but known working).

    Also, what OS and Java version are you using?

    And what target OS and architectures do you plan to use? This matters because a JNI lib must be built as Universal Binary for multiple archs, if you want to target other archs. If you're not at that point yet, it's OK, just something to think about.
  3. LD220 thread starter macrumors member

    Jun 3, 2009
    Thanks for your reply. When I tried your first command I got an error:
    marissas-macbook:JNI Marissa$ gcc -x c \
    > -I/System/Library/Frameworks/JavaVM.framework/Headers \
    > -c firstJNI.c -o firstJNI.o
    firstJNI.c:1: error: syntax error before ‘<’ token

    I'm running Mac OS X 10.5.8 with Java 1.5.0_22. Right now I just plan to run the examples on my Mac, but if I go any further I may need to have a program that runs on just Windows XP and 7.
  4. chown33 macrumors 604

    Aug 9, 2009
    Your C source file probably contains a misplaced <pre> at its head, and </pre> at its end. When I visited the linked article, that's definitely what I was in his posted code.

    Regarding 10.5.8, there are two runnable versions of Java 5: 32-bit and 64-bit. It will probably pick the right JVM, but your compiled JNI lib must match the JVM. If it complains about no lib when you run it, or not the right architecture, post again with the exact error message.
  5. LD220 thread starter macrumors member

    Jun 3, 2009
    Thanks a lot! You were right, I thought I had removed all the <pre>'s, but one seemed to have snuck in there. I ran it and it worked fine. I'll have to take a look at the code and make sure I understand it now.

    Do you by any chance know of another good tutorial out there?
  6. chown33 macrumors 604

    Aug 9, 2009
    Afraid not. I learned JNI by reading the book and trying things. Trying LOTS of things. Start simple and work up.

    The book is "Java Native Interface: Programmer's Guide and Specification". There are at several versions. Code written for one version works on later versions, in my experience. Code written for a later version will NOT work on earlier versions.

    This one is for JDK 1.4 and earlier:

    And the 1.5 version:

    And 1.6:

    You can get free downloads at the above links. I grabbed the PDF and HTML versions, and even printed a copy out so I can scribble on it. You can also buy a nicer printed and bound copy.

Share This Page