Tuesday, August 26, 2014

Simple JNI program tutorial in Mac OS X Maverics

Hi, I was digging into JNI for some work, and I saw several examples on the internet. But I had problems successfully running a simple Hello World program in Mac OS X mavericks, with the renovated clang compiler.

This tutorial is based on the following two online tutorials, the first one is the easier one but only for windows based compilers, the second one actually lists all the required linker options for Mac.

http://www.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html
https://thenewcircle.com/static/bookshelf/java_fundamentals_tutorial/_java_native_interface_jni.html

As I have come out successful after some digging, here I am presenting the complete code I used. The code listing works with default clang, or gcc compiler installed through mac port.

I intentionally presented screenshot instead of writing the code here, so that viewers write the code themselves, instead of copying!!

First we write a Hello World Program in Java,



Then compile it

Now create the required header file, C/C++

Now, implement HelloJNI.c


Now, this is important, the hello library needs to be with the jnilib extension in Mac, not the linux traditional .so extension. So, it will be libhello.jnilib
The whole command will be,

Now, just run it like,
That's it!

-->

No comments: