Java Native Interface (JNI)
JNI is the native programming interface for java that is part of JDK.
Using JNI you can operate with other applications and libraries written in other language
such as C,C++.
But the basic question arises when should I use the JNI ?
- You want some platform specific information and the standard Java class
library may not support the platform-dependent features needed by your
application.
-
You have some library application written in other language and
you want to use it in your java application.
- You want Java should interact with some low level programming language.
Below is given Simple Example
See that methods have 'native' KeyWord
public native void displayHelloWorld();
public native void displayOther();
private native String getLine(String prompt);
The DLL we are going to use is firstJNI.DLL
This DLL can be generated by VC++ or borland.
Which we will discuss later.
Compile the above code using
prompt>javac firstJNI.java
Then create header File using
prompt>javah javah -jni HelloWorld
This will create firstJNI.h file
In the header File you will see
-------------------------------------
JNIEXPORT void JNICALL Java_firstJNI_displayHelloWorld
(JNIEnv *, jobject);
/*
* Class: firstJNI
* Method: displayOther
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_firstJNI_displayOther
(JNIEnv *, jobject);
/*
* Class: firstJNI
* Method: getLine
* Signature: (Ljava/lang/String;)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_firstJNI_getLine
(JNIEnv *, jobject, jstring);
----------------------------------------------
Don't edit header File
Now the real part comes - making the DLL!. First we see
generating the DLL using the Borland compiler.
Now we will write some simple C Code
In the above code, the first two methods only Display message,
while the third one takes input from the user
The Syntax For Declaring Methods
--------------------
JNIEXPORT <return type> JNICALL
Java_<className>_<methodName>(JNIEnv *env, jobject obj)
{
...........
}
-------------------
To generate DLL
create bcc32.cfg file in %BORLAND_INSTALL%/BCC5/bin Folder
In the bcc32.cfg file add following code
-I"%BORLAND_INSTALL%\BCC55\include;%JAVA_HOME%\include;%JAVA_HOME%\include\win32"
-L"%BORLAND_INSTALL%\BCC55\lib;%BORLAND_INSTALL%\BCC55\Lib\PSDK;"
i.e the path for borland compiler and java.
now goto prompt and say
prompt>bcc32 -tWM -tWD firstJNI.c
This will create firstJNI.lib File
Now say
prompt>implib -c firstJNI.lib firstJNI.dll
That's it!!
It will generate DLL file.
Now you can run the firstJNI.class file by using java.
--------------------
Now let see how to generate DLL using VC++
Click File->New->Win32Dynamic-Link Library
Give name and Select A simple DLL project
You will have
firstJNI.CPP file
Below is given the firstJNI.cpp file
The syntax declaration is same as discussed above. In VC++ it
may throw error for unable to include for jni.h or
firstJNI.h where you have to give path for these header files while including.
The above code will also check for RAM size and print it.
Borland compiler do not provide bios.h so it is not included in the code.
Build all Application and you will get the DLL file.
Go to prompt and run firstJNI.class using java.
So here we can print RAM size!!!
Now since this is the basic we can go further with this.
If you don't set the library path correctly it will throw exception java.lang.UnsatisfiedLinkError: no hello in shared library path
at java.lang.Runtime.loadLibrary(Runtime.java)
at java.lang.System.loadLibrary(System.java)
at
at java.lang.Thread.init(Thread.java)
so copy library(dll) in same folder.
|
Useful Links

|
|