Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

LD220

macrumors member
Original poster
Jun 3, 2009
35
0
Hi everyone. I'm attempting to learn a little bit about Java JNI and I wanted to run this example on my mac (http://www.codetoad.com/java_simpleJNI.asp). 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.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,753
8,441
A sea of green
If all you need are the C compiling commands:
Code:
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:
Code:
    System.loadLibrary( "Foo" );
not:
Code:
    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.
 

LD220

macrumors member
Original poster
Jun 3, 2009
35
0
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.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,753
8,441
A sea of green
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.
 

LD220

macrumors member
Original poster
Jun 3, 2009
35
0
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?
 

chown33

Moderator
Staff member
Aug 9, 2009
10,753
8,441
A sea of green
Do you by any chance know of another good tutorial out there?

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:
http://java.sun.com/docs/books/jni/

And the 1.5 version:
http://java.sun.com/j2se/1.5.0/docs/guide/jni/

And 1.6:
http://java.sun.com/javase/6/docs/technotes/guides/jni/

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.