Something About JNI
JNI allows one to write native methods to handle situations when an application cannot be written entirely in the Java programming language, e.g. when the standard Java class library does not support the platform-specific features or program library.
It can be used to interface with code written in other languages, such as C and C++. It is also used for time-critical calculations or operations like solving complicated mathematical equations, since native code can be faster than JVM code.
How the JNI works
In the JNI framework, native functions are implemented in separate .c or .cpp files. (C++ provides a slightly cleaner interface with JNI.) When the JVM invokes the function, it passes a JNIEnv pointer, a jobject pointer, and any Java arguments declared by the Java method. A JNI function may look like this:
JNIEXPORT void JNICALL Java_ClassName_MethodName
(JNIEnv *env, jobject obj)
{
/*Implement Native Method Here*/
}
The env pointer is a structure that contains the interface to the JVM. It includes all of the functions necessary to interact with the JVM and to work with Java objects. Example JNI functions are converting native arrays to/from Java arrays, converting native strings to/from Java strings, instantiating objects, throwing exceptions, etc. Basically, anything that Java code can do can be done using JNIEnv, albeit with considerably less ease.
For example, the following converts a Java string to a native string:
//C++ code
JNIEXPORT void JNICALL Java_ClassName_MethodName
(JNIEnv *env, jobject obj, jstring javaString)
{
//Get the native string from javaString
const char *nativeString = env->GetStringUTFChars(javaString, 0);
//Do something with the nativeString
//DON'T FORGET THIS LINE!!!
env->ReleaseStringUTFChars(javaString, nativeString);
}
/*C code*/
JNIEXPORT void JNICALL Java_ClassName_MethodName
(JNIEnv *env, jobject obj, jstring javaString)
{
/*Get the native string from javaString*/
const char *nativeString = (*env)->GetStringUTFChars(env, javaString, 0);
/*Do something with the nativeString*/
/*DON'T FORGET THIS LINE!!!*/
(*env)->ReleaseStringUTFChars(env, javaString, nativeString);
}
/*Objective-C code*/
JNIEXPORT void JNICALL Java_ClassName_MethodName(JNIEnv *env, jobject obj, jstring javaString)
{
/*DON'T FORGET THIS LINE!!!*/
JNF_COCOA_ENTER(env);
/*Get the native string from javaString*/
NSString* nativeString = JNFJavaToNSString(env, javaString);
/*Do something with the nativeString*/
/*DON'T FORGET THIS LINE!!!*/
JNF_COCOA_EXIT(env);
}
Note that C++ JNI code is syntactically slightly cleaner than C JNI code because like Java, C++ uses object method invocation semantics. That means that in C, the env parameter is dereferenced using (*env)-> and env has to be explicitly passed to JNIEnv methods. In C++, the env parameter is dereferenced using env-> and the env parameter is implicitly passed as part of the object method invocation semantics.
Native data types can be mapped to/from Java data types. For compound types such as objects, arrays and strings the native code must explicitly convert the data by calling methods in the JNIEnv.
Mapping types
The following table shows the mapping of types between Java and native code.

In addition, the signature "L fully-qualified-class ;" would mean the class uniquely specified by that name; e.g., the signature "Ljava/lang/String;" refers to the class java.lang.String. Also, prefixing [ to the signature makes the array of that type; for example, [I means the int array type.
Here, these types are interchangeable. You can use jint where you normally use an int, and vice-versa, without any typecasting required.
However, mapping between Java Strings and arrays to native strings and arrays is different. If you use a jstring in where a char * would be, your code could crash the JVM.
// !!! Incorrect !!! //
JNIEXPORT void JNICALL Java_ClassName_MethodName
(JNIEnv *env, jobject obj, jstring javaString)
{
printf("%s", javaString);
}
// Correct //
JNIEXPORT void JNICALL Java_ClassName_MethodName
(JNIEnv *env, jobject obj, jstring javaString)
{
//Get the native string from javaString
const char *nativeString = env->GetStringUTFChars(javaString, 0);
printf("%s", nativeString);
//DON'T FORGET THIS LINE!!!
env->ReleaseStringUTFChars(javaString, nativeString);
}
This is similar with Java arrays, as illustrated in the example below that takes the sum of all the elements in an array.
// !!! Incorrect !!! //
JNIEXPORT jint JNICALL
Java_IntArray_sumArray(JNIEnv *env, jobject obj, jintArray arr)
{
int i, sum = 0;
for (i = 0; i < 10; i++) {
sum += arr[i];
}
return sum;
}
// Correct //
JNIEXPORT jint JNICALL
Java_IntArray_sumArray(JNIEnv *env, jobject obj, jintArray arr)
{
jint buf[10];
jint i, sum = 0;
env->GetIntArrayRegion(arr, 0, 10, buf);
for (i = 0; i < 10; i++) {
sum += buf[i];
}
return sum;
}
Of course, there is much more to it than this. Look for links below for more information.
JNIEnv*
A JNI interface pointer (JNIEnv*) is passed as an argument for each native function mapped to a Java method, allowing for interaction with the JNI environment within the native method. This JNI interface pointer can be stored, but remains valid only in the current thread. Other threads must first call AttachCurrentThread() to attach themselves to the VM and obtain a JNI interface pointer. Once attached, a native thread works like a regular Java thread running within a native method. The native thread remains attached to the VM until it calls DetachCurrentThread() to detach itself.
To attach to the current thread and get a JNI interface pointer:
JNIEnv *env; (*g_vm)->AttachCurrentThread (g_vm, (void **) &env, NULL);
To detach from the current thread:
(*g_vm)->DetachCurrentThread (g_vm);
--------------------------------------------------------------------------------------------------------------------------------------------------------
About Borland Compilation
This is a tutorial which told you, how to use JNI.
Below are the steps to use JNI Simple Hello world Program.
1. You have to install the Borland C++ Compiler(C++ Compiler 5.5). It can be downloaded from :- Borland C++ Compiler.
2. After Installation put Borland bin path in Environmental Variables:- "F:\Borland\bin".
3. Change your Borland "F:\Borland\Bin\bcc32.cfg" file like Below
-I"F:\Borland\include;C:\Program Files\Java\jdk1.6.0_02\include;C:\Program Files\Java\jdk1.6.0_02\include\win32" -L"F:\Borland\lib;F:\Borland\Lib\PSDK"
4. Write the Java File
class MyJNI {
public native void displayHelloWorld();
static {
System.loadLibrary("MyJNI"); //This is MyJNI.dll
}
public static void main(String[] args) {
MyJNI obj=new MyJNI();
obj.displayHelloWorld();
}
}
5. Compile the Java File by Command:-
javac MyJNI.java
6. Next task is to create the Header file from Java File. To create that use following command:-
javah -jni MyJNI
This will create MyJNI.h. The Content of MyJNI.h file will be as follow. Do not Edit this file as it is generated by machine.
/* DO NOT EDIT THIS FILE - it is machine generated */ #include/* Header for class MyJNI */ #ifndef _Included_MyJNI #define _Included_MyJNI #ifdef __cplusplus extern "C" { #endif /* * Class: MyJNI * Method: displayHelloWorld * Signature: ()V */ JNIEXPORT void JNICALL Java_MyJNI_displayHelloWorld (JNIEnv *, jobject); #ifdef __cplusplus } #endif #endif
7. Now importent work to implement the C code. That is shown below:-
/*MyJNI.c*/ #include#include "MyJNI.h" #include JNIEXPORT void JNICALL Java_MyJNI_displayHelloWorld(JNIEnv *env, jobject obj) { printf("Hello world!\n"); return; }
The JNI function shown in underscore format as above.
8. To Compile this C Code we are using the borland Compiler. The Command will be as below.
F:\Java\JNI\MyEx>bcc32 -tWM -tWD MyJNI.c Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland MyJNI.c: Warning W8057 MyJNI.c 10: Parameter 'env' is never used in function Java_MyJNI_displayHelloWorld Warning W8057 MyJNI.c 10: Parameter 'obj' is never used in function Java_MyJNI_displayHelloWorld Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
9. This will create the files :- MyJNI.dll, MyJNI.obj, MyJNI.tds
10. To create Library file for use we have following command:-
implib -c MyJNI.lib MyJNI.dll
This will create the file:- "MyJNI.lib".
11. Now Final step is to run this JNI Program.
F:\Java\JNI\MyEx>java MyJNI Hello world! F:\Java\JNI\MyEx>
12. You can also download the source code here.
Now you can use this libary with dll any where you want.
This is most easy way to use JNI rather than going to use VC++ as C Compiler.
Enjoy Have a nice Day.
For more information visit Dwij IT Solutions.
No comments:
Post a Comment
Your comments will be important to impprove this blog.
Please have some comments or doubts freely.