Begining with JNI

                                                 Java Native Interface (JNI)    jni

Java provides a framework called the Java Native Interface (JNI), with which it is possible to write native methods. A native method is a method that is mapped to a function in a library of native code, such as a DLL under Windows. Or, to put things more simply, you can write code in C or C++1 or any other language and call it from Java.

Purpose and features

JNI enables programmers 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 is also used to modify an existing application—written in another programming language—to be accessible to Java applications. Many of the standard library classes depend on JNI to provide functionality to the developer and the user, e.g. file I/O and sound capabilities. Including performance- and platform-sensitive API implementations in the standard library allows all Java applications to access this functionality in a safe and platform-independent manner.

The JNI framework lets a native method use Java objects in the same way that Java code uses these objects. A native method can create Java objects and then inspect and use these objects to perform its tasks. A native method can also inspect and use objects created by Java application code.

JNI is sometimes referred to as the “escape hatch” for Java developers because it enables them to add functionality to their Java application that the standard Java APIs cannot otherwise provide. 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, because for some classes of problems native code may run 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 simpler 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.

 

The argument obj is a reference to the Java object inside which this native method has been declared.

 

For example, the following converts a Java string to a native string:

 

//C++ code

extern “C”

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);

}

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.

JNI and OSGi

The following graphic shows the Java Native Interface (JNI) that allows native code (C or C++) to interact with bytecode. The OSGi Framework is a dynamic module system that provides a way to deploy a new version of a software module without having to reboot the container of the modules. One use case is to automate updates for the industrial internet, such as:

  • telecommunications devices that have many modules (camera, video player, audio recording, audio player, web browser, text input, text display), or
  • devices that serve medical purposes. For example, a CAT-scanner uses software to gather and to analyze x-ray data. A new version of the analytical software could be provided without having to shut down the acquisition system. Another use case involves the separation of the database layer from the business rules layer from the view layer. (Compare to the Model-View-Controller pattern.)
  • osgi

Pushpraj Kumar….