Java

Getting started with Java Native Interface (JNI)

Java Native Interface, abbreviated JNI, is a programming interface that lets Java developers run codes and snippets from other programming languages in Java. It comes coupled with the essential Java Software Development Kit, or SDK (we’ll cover that in a different tutorial).

JNI is also revered for its feature to call on API to sneak in a Java Virtual machine in Java applications. This enables the devs to invoke the java code within the code of the native application.

If you’ve spent some time working with java, you’ve probably already come across the many performance issues that inevitably come your way. This isn’t a problem when you run the same code in the native language, which can perform over eighteen times faster when read in a compiled model. In addition to that, you can also use outdated/incompatible hardware routines with native codes in other languages.

This tutorial will demonstrate how the machine C/C++ code can be invoked from within a java application.

Prerequisites

You’re going to need a few things to follow through with this guide properly. These include the Java compiler, or Javac.exe, along with the JVM, as well as the native method C generator(javah.exe). All three of these come built into the software development kit, so you’re all good if you have that. Besides these three, you’ll also need the files that define JNI, including Native header files and complete library files.

And of course, in a tutorial about running the C and C++ codes, we’ll also use the C compiler to create a shared library.

JNI Components

JNI is mainly driven by two components, namely h and javah. H is the header file component that replaces native codes with java code, whereas Javah makes it so that this file can be loaded to app header files by itself.

Invoking C/C++ from Java code

Step 1: Writing the code in Java

The code is first written in java and is compliant with three conditions. First, it is written with the native method to be invoked later on. Secondly, it has to load the shared library of which the native code is part, and lastly, it has to invoke the native methods.

Let’s use this code to illustrate further:

Notice lines 3 and 6; these are the lines where the native methods are included. The code that loads the shared libraries is located on line 10, which leads to the method being invoked between lines 12 to 15.

Step 2: Compiling the java code to bytecode

The second step has to do with compiling the java code. The javac compiler can do the job here for us; just issue the command below:

$javac Example1.java

Step 3: Create C/C++ header files

Next, the native language header files have to be created. This header files powers the signatures of the native codes.

These header files can be created with the javah native tool, a C stub generator bundled with SDK, using the following command:

javah Example1

The following output should return:

Step 4: Writing the native code

This is where we’ll write C/C++ code. You should note all the signatures that have to resemble the declarations we made in step 1.

Following is an implementation written in C language:

Step 5: Create a shared library

A shared library can be created with any compiler. Because the shared library contains the native code, we’ll have to create one.

Step 6: Launch your program

This step involves assessing the code and identifying any problems with the program. This is going to involve the Java runtime environment because the code is going to execute in JVM primarily.

Issue the following command:

java Example1

It should return:

So that was our short beginner’s guide to using Java Native Interface. We hope you’ve found it helpful.

Learning how to work with JNI is essential for anyone who wants to develop Java applications, especially android applications for smartphones.

About the author

Younis Said

I am a freelancing software project developer, a software engineering graduate and a content writer. I love working with Linux and open-source software.