Java Native Interface (JNI)
Java Native Interface (JNI) is one of the intersting interface by java By using Java Native Interface (JNI) you can operate with other applications and libraries.
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 ?
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.
//firstJNI.java
class firstJNI
{
public native void displayHelloWorld();
public native void displayOther();
private native String getLine(String prompt);
static {
System.loadLibrary("firstJNI");//This is firstJNI.DLL
/*if generated by borland
System.loadLibrary("firstjni");//This is firstjni.dll
*/
}
public static void main(String[] args)
{
firstJNI JN=new firstJNI();
JN.displayHelloWorld();
JN.displayOther();
String input = JN.getLine("Enter Some Thing ");
System.out.println("You Entered " + input);
}
}
Compile the above code using (What does this mean ?)
prompt>javac firstJNI.java
Then create header File using (What does this mean ?)
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 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
// MYVCDLL.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include "D:\Kanad\Study\codeToad Articles\firstJNI.h"
#include "jni.h" //can copy or give full path
#include <math.h>
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
extern "C" __declspec(dllexport) int getMemorySize();
//And your function definition should look like this:
extern "C" __declspec(dllexport) int getMemorySize()
{ //do something
MEMORYSTATUS memoryStatus;
int MB=1024*1024 ;
double memSize;
memoryStatus.dwLength=sizeof(MEMORYSTATUS);
GlobalMemoryStatus(&memoryStatus);
__int64 size= memoryStatus.dwTotalPhys;
memSize=(double)size/MB;
printf("\nTotal Memory %.0lf MB",ceil(memSize));
return 0;
}
JNIEXPORT void JNICALL
Java_firstJNI_displayHelloWorld(JNIEnv *env, jobject obj)
{
printf("Hello world! This is using VC++ DLL\n");
}
JNIEXPORT void JNICALL
Java_firstJNI_displayOther(JNIEnv *env, jobject obj)
{
printf("Hello world! This is using VC++ DLL Other Function \n");
getMemorySize();
}
JNIEXPORT jstring JNICALL
Java_firstJNI_getLine(JNIEnv *env, jobject obj, jstring enter)
{
char buf[128];
const char *str = env->GetStringUTFChars(enter, 0);
printf("\n%s", str);
env->ReleaseStringUTFChars(enter, str);
scanf("%s", buf);
return env->NewStringUTF(buf);
}
Now I have questions about how can I use .dll file written in C++/C in my java application. I am developing application for android using Eclipse and I have some dll files and I haven't their source ... How can I use them in my project ???
To use an arbitrary DLL from Java you usually have to create an adapting DLL with the conventions of JNI that itself loads the "target" DLL and calls the required functions. To generate the correct headers for your adapter DLL you can use the tool javah shipped with the JDK.
First a disclaimer - I'm a bit sketchy on this, it's been a while since I've used JNI.
Many JNI examples assume you own the code for the library you want to call, which in my experience is rarely the case. In the example you sight the javah util has been used to generate a header file, against which cpp implementation has been written - this is why you can see the jni header file and various Java keywords in the cpp file.
In order to use a 3rd party dll, you first need the documentation for that dll, without that you're dead in the water. The reason you need the documentation is that you're going to provide a wrapper dll that simply delegates to the 3rd party dll - you need to know how to call it and how to perform any type mappings. Obviously it's this wrapper that will contain all the JNI stuff to allow Java to make the call to that wrapper, which in turn calls the 3rd party dll.
There's various ways to do this but the easiest way I know is to use SWIG, which will generate all the C++ code required for the wrapper dll. It also helps to have someone that knows C++ on hand - they'll be invaluable writing interface files (.i or .swg files) that SWIG uses to generate the wrapper code.
I'm not a expert on these but i think you should use System.loadLibrary("comparejni");
http://blog.jayway.com/2010/01/25/boosting-android-performance-using-jni/
Hope it helped
I don't think you're going to be able to use a Windows native library (.dll) in an Android application. To load on Android, the code would need to be built into a .so.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With