About Java Native Interface

Posted By : Kiran Sharma | 27-Apr-2018

Its very useful java programming framework. This interface allows a code written in java language to be used by native applications which have platform and hardware dependent programs and vice-versa. For e.g We need to write an application that can't be completely written in java language and the reason can be java class library does not support the platform-specific features or program library, so we can write native methods to handle such situations.

 

This framework lets a native method use Java objects in the same manner that Java code uses these objects. A native method can create Java objects and use these objects to perform its tasks. A native method can also use objects created by Java application code. An application that uses JNI loses the platform portability feature provided by Java .

 

Design and implemenation

*********************************

 

Step 1 :Write a java class “DemoJNI.java” which uses c/c++ code.

 

 
public class DemoJNI {
	static {
		System.loadLibrary("demo"); // 1
	}

	private native void callDemo(); // 2

	public static void main(String[] args) {
		new DemoJNI().callDemo(); // 3
	}
}

 

 

 

Here in the above code, static block is loading native library by using loadLibrary method of System class. In this native library we have mentioned native method called callDemo(). To declare a native method in our java program we are using native keyword in line 2. A native method does not have body. Native method's implementation is defined in native library “demo” which we have loaded in static block. In main main method we are calling that native method as we do in java.

 

Step 2 : Now we need to compile our java program and we need to generate a c/c++ header file. The header file will be create with the name of java file with extension .h .In java8 these both processes can be done using one single command-

 

 javac -h . DemoJNI.java
        

 

Prior to JDK8 the process was -

 

javac DemoJNI.java
javah  DemoJNI
        

 

 

The generated header file would be something like -

 

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class DemoJNI */

#ifndef _Included_ DemoJNI
#define _Included_DemoJNI
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     DemoJNI
 * Method:    callDemo
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_DemoJNI_ callDemo
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif DemoJNI.c
        

 

In the above code, the extern "C" is recognized by C++ compiler only.

 

Step3: Implementation of native method.

 

Here we have given native method implementation in “DemoJNI.c”.

 

#include <jni.h>
#include <stdio.h>
#include "DemoJNI.h"
 
// Implementation of native method callDemo() of DemoJNI class
JNIEXPORT void JNICALL Java_DemoJNI_callDemo(JNIEnv *env, jobject thisObj) {
   printf("Hello World!\n");
   return;
}
        

 

Step4: Compile the c program.

 

Compile the C program HelloJNI.c into share module libhello.so using gcc.

 

The below command works for (Ubuntu) 64-bit JDK-

 

gcc -fPIC -I"$JAVA_HOME/include" -I"$JAVA_HOME/include/linux" -shared -o libdemo.so DemoJNI.c

        

 

Step5: Run the java program

 

java -Djava.library.path=. DemoJNI

 

Output: 

 

Hello World!
        

About Author

Author Image
Kiran Sharma

Kiran has good knowledge of java with Servlets, JSPs, Spring, and hibernate frameworks. She is very honest towards her work. Her hobby is listening to music.

Request for Proposal

Name is required

Comment is required

Sending message..