Java Native Interface Program

By | June 5, 2016

Java Native Interface Program with C Code

Learn How To Build Java Native Interface Program with C Programming Language. This JNI Tutorial will help you to execute Java and C Programs together on Linux Terminal. Also, you will get to know how to configure your system to execute JNI Codes in Linux Ubuntu. We have mentioned complete and detailed steps to write a Java Native Interface Code.

Remember

This JNI Tutorial should be executed in Oracle Java 8. Other Java versions have certain incompatibilities that will hamper program execution. To Download Open JDK 8, type the following command in the Linux Ubuntu Terminal:

sudo apt install openjdk-8-jdk

If you have multiple versions of Java installed in your machine, you will have to point the default java version. For this, you will have to switch the version and point it as the default version.

To view all the Java versions installed in your Linux Operating System, follow the commands:

update-java-alternatives --list

The output would be something of this sort:

java-1.7.0-openjdk-amd64 1071 /usr/lib/jvm/java-1.7.0-openjdk-amd64
java-1.8.0-openjdk-amd64 1069 /usr/lib/jvm/java-1.8.0-openjdk-amd64

Now, you need to select the openJDK 8 and set it as the default version. Type the following commands sequentially to set Java 8 as the default version.

sudo update-java-alternatives --set java-1.8.0-openjdk-amd64
sudo apt-get install oracle-java8-set-default

Also Read: Difference Between C++ and Java

Java Code For Native Interface Program To Print Hello World

public class JNIProgram
{
    int x;
    public static void main(String args[])
    {
        JNIProgram object1 = new JNIProgram();
        object1.x = 545;
        System.out.println(object1.x);
        object1.display();
        System.out.println(object1.x);
    }
    public native void display();
    static
    {
        System.loadLibrary("p");
    }
}

Java Header File Code

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

#ifndef _Included_JNIProgram
#define _Included_JNIProgram
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     JNIProgram
 * Method:    display
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_JNIProgram_display
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

C Program For Java Native Interface To Print Hello World

#include"stdio.h"
#include"JNIProgram.h"
#include"jni.h"

JNIEXPORT void JNICALL Java_JNIProgram_display
(JNIEnv *env, jobject object1)
{
    jint x; 
    jfieldID field_id;  
    jclass class_var;
    printf("nHellon");
    class_var = (*env)->GetObjectClass(env,object1);
    field_id = (*env)->GetFieldID(env,class_var,"x","I");
    x = (*env)->GetIntField(env,object1,field_id);
}

Also Read: Print System Date and Time in Java

 

Linux Ubuntu Terminal Commands for JNI Program Execution

The chances of getting compilation errors are high in JNI due to invalid configuration settings. Follow the steps to run Java Native Interface Programs in Linux Terminal.

Java Compilation

javac JNIProgram.java

Java Header File

javah JNIProgram

C Program Compilation with Object Creation

gcc -I/usr/lib/jvm/java-8-oracle/include/ -I/usr/lib/jvm/java-8-oracle/include/linux -O -fpic -c JNIProgram.c

Linker Command

gcc -shared -o libp.so JNIProgram.o

Final Execution Statement

 
java -Djava.library.path=. JNIProgram

Also Read: Java Program To Check Public IP Address

Output

Java Native Interface Program with C Language

We hope that you could successfully execute the Java Native Interface Program. In case you have any compilation error or doubt about this JNI Tutorial, let us know about it in the comment section below.

Let's Discuss