segunda-feira, janeiro 12, 2009

How to build JNI applications using Dev-C++ and Netbeans

JNI is an acronym of Java Native Interface. It is a programming framework that allows your Java code to call and to be called by native applications. So, you can write libraries in other languages, such as Assembly, C/C++, or even C#.

Here, we are going to talk about how to write a DLL in C/C++, using Dev-C++ 4.9.9.2 (avaliable at: http://www.bloodshed.net/devcpp.html). Dev-C++ is not upgraded for a long time, so feel free to use CodeBlocks (the process of building a DLL is almost the same. You can download Codeblocks at: http://www.codeblocks.org). I used Dev-C++ because it seemed nice to me.

Before we start, let's talk about some advantages and disadvantages of JNI:

Using JNI you can...:
* Call Windows API functions.
* Increase execution speed.
* Call API functions of some product which is in C or C++, from Java Client.

Some disadvantages:
* Forget "write once run anywhere" (your code is not portable between different platforms).
* It is difficult to debug runtime error in native code.
* There is a potencial security risk.
* Only applications and signed Applets can invoke the JNI.
* There is no garbage collection at the JNI side (This means that you must dealloc the used memory your code previously alloced).

Now that you know the advantages and risks, let's show an example about how to use JNI with Dev-C++ and Netbeans.

First, configure Dev-C++. Go to Tools -> Compiler Options and create a new compiler set.



Now, go to the Directories tab (as shown bellow) and add the include paths of JDK to C and C++ (be careful here! As I said, you should add to C and then to C++ !!!).

You should add the following directories (you can find them in the JDK folder):
C:\Program Files\Java\jdk1.6.0_07\include
C:\Program Files\Java\jdk1.6.0_07\include\win32

Those are the folders of my computer! They can change according to your JDK version, OS languaage/partition. Be careful!
If you are curious, inside those folders you can find the jni.h file, that you are going to need ahead.

Now that you configured Dev-C++, go to File -> New -> Project (at the top menu), and create a DLL project.


Then, delete the files that came with the project (in the example: dllmain.cpp and dll.h).


Now, create new files in the project (Example.h and Example.cpp).




Here you can see the JNI side code. The header files and functions should be written as shown. The declaration of a function is composed by the following: Java_PackageName_ClassName_methodName (look again to the images above).
To create a DLL, simply Compile your DLL project (in the top Menu, go to: Execute -> Compile).

Finally, create a Java Application at NetBeans (avaliable at: http://www.netbeans.org). At the java project, you should create a package equals to PackageName and a class equals to ClassName.
Copy the DLL into your java project folder.



Now, make a reference to your DLL using System.loadLibrary method. To call a native method, you should create a "signature" method on Java side, using the native keyword. See the code bellow:



Now, compile you class and run it! :-)

Have fun! =]