Skip to main content

With simple 5 steps, you can do JNI programs

Hi guys, with simple five steps you can do Java JNI programs. Let's start with what is JNI?

JNI 

  • The Java™ Native Interface (JNI) is a powerful feature of the Java platform. 
  • Applications that use the JNI can incorporate native code written in programming languages such as C and C++, as well as code is written in the Java programming language. 
  • The JNI allows programmers to take advantage of the power of the Java platform, without having to abandon their investments in legacy code. 
  • Because the JNI is a part of the Java platform, programmers can address interoperability issues once and expect their solution to work with all implementations of the Java platform.

Installation

  1. Install JAVA recent JDK in your local machine 
  2. Install an IDE  which is comfortable for you eg: Netbeans, EclipseJetBrains
  3. Install cross compiler which will help to integrate two different platforms. eg: Cygwin 

Steps to do JNI Programs

  1. Create new java project in NetBeans. File --> New Project --> java application and declare the native methods. Here addingSum is the native method its definition will available in C Project.

Creation of Java project in Netbeans



2. Compile the above java project which will create the corresponding class files those files will help in creating header files.


Class file Created after compilation.


Header file syntax : 
C:\Users\UserName\Documents\NetBeansProjects\AddingSum\src>"C:\Program Files (x86)\Java\jdk1.8.0_11\bin\javah.exe" -o sumheader.h -jni -classpath ..\build\classes MainJava

Above header file syntax type in command prompt like this:

Header file creation syntax


3. Create a new C project. File --> New Project -->C /C++ application. Drag n drop the header file which we created under the java project to C project and inside the header file system will generate the native method signature, copy that method signature and paste in main.c under C application.

Drag and Drop the header file from java project to c project

Inside the generated header file, highlighted the native method signature


4. After copying method signature inside the main.c, start creating the instance (JNIEnv *env, jobject jobj,jint a , jnit b) and write the logic of your native method.

The logic of the native method

5. Compile your C Project, Dynamic Link Libary will be created just map that DLL library in the java project again like this: 

DLL creation after a successful build 

Copy the DLL path inside the java file


Congratulations, You completed your first JNI programs within 5 steps 👌. 
If you have any doubts about this article, please leave your comments.See you in next post 😇

Comments

  1. Hello Lavanya,

    This is Sachin. I was going thru your both blogs and would like to discuss something which you might be interested in. Kindly prompt me on my email address in case you see my message and would like to contact me.
    E-Mail= srivastavasachin120@gmail.com

    ReplyDelete

Post a Comment