|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionIn this tutorial, I will try to explain how to call native functions which are written in C or C++ from Java, using JNI. What is JNIJNI is an acronym of Java Native Interface. Using JNI, we can call functions which are written in other languages from Java. Advantages and disadvantages of this method are as follows: Advantages:
Disadvantages:
Here we will try to call the MFC function //Test1.java
class Test1
{
static
{
System.loadLibrary("TestDll");
}
public static void main(String ar[])
{
System.out.println("Hello world from Java");
Test1 t=new Test1();
t.inDll();
}
public native void inDll();
}
In static block, we are loading a DLL named TestDll.dll which should be placed in <WINDIR>/SYSTEM32 directory. I am not saying you cannot put this DLL in other directories, you can, but then you will have to register that path in JVM. Here you can call another method as well, instead of System.load(“c://winnt//system32//TestDll.dll”);
I think public native void inDll();
As you can guess, this native keyword specifies that Compile this class normally. Now we will need to generate header file of this class. You can use javah.exe utility which is included with JDK. Javah -jni Test1
Header file is generated in current directory with the name Test1.h. Keep this header file safe as we will use this soon. Now we are going to write a DLL which will contain the implementation of our native function. I have chosen MFC AppWizard (DLL) in project and named my project: TestDll. Include your class header file which was generated previously using javah.exe in TestDll.h. Copy following line from Test1.h and paste it at the end of TestDll.h. JNIEXPORT void JNICALL Java_Test1_inDll(JNIEnv *, jobject);
Change this line to: JNIEXPORT void JNICALL Java_Test1_inDll(JNIEnv *env, jobject obj)
{
AfxMessageBox("Hello World from dll");
}
Compile this DLL, you will get an error from compiler because compiler was unable to find jni.h file which is included in Test1.h. So include the following two directories in IDE settings.
Go to Project/Settings, on C/C++ tab, go to Project option, insert following lines at the end: /I "<Drive>:\<JDK Directory>\include "
/I "<Drive>:\<JDK Directory>\include\win32"
Now compile and generate your DLL, put newly created DLL in system32 directory and then run your class file. It should work properly. If you receive an error like In this tutorial, I have tried to explain the basics of JNI. I will write a second tutorial of the series soon in which I will explain how to pass parameters to native functions and return something, along with a practical example. Waiting for your comments and questions.
|
||||||||||||||||||||||