Java Native Interface ( JNI ) Example , Calling Native C functions from JAVA

The Java Native Interface (JNI) establishes a well-defined and platform-independent interface between the JAVA and Native C program / library. Using JNI we can access the native C API’s from JAVA applications.

pre-requisite – Install JAVA using “How to Install Java using apt-get command on Ubuntu ?”

Write a JAVA class [ ]

public class HelloWorld { 

static { 

public native void printHelloWorld();


Compile the class using javac

$ javac

after this HelloWorld.class file should get created.

Create header file required for writing a native program

$ javah -jni HelloWorld

Above command will create a C header file HelloWorld.h

Write a helloworld.c native c program.

#include "HelloWorld.h"
#include <stdio.h>

JNIEXPORT void JNICALL Java_HelloWorld_printHelloWorld (JNIEnv *env, jobject obj) { 
        printf("Hello World\n");

Create a shared library.

$ gcc -I/usr/lib/jvm/java-11-openjdk-amd64/include/ -I/usr/lib/jvm/java-11-openjdk-amd64/include/linux/ -shared -o helloworld.c

We need to add include paths “usr/lib/jvm/java-11-openjdk-amd64/include/” and “/usr/lib/jvm/java-11-openjdk-amd64/include/linux/” for the jni.h and jni_md.h headers respectively.

Write a Main java class[ ], which will call native function.

public class Main {

        public static void main(String[] args) {
                new HelloWorld().printHelloWorld();



$ javac

Now set the LD_LIBRARY_PATH so that the C shared library can be found by the JAVA executable as,


Run the program

$ java Main

1) JNI Example

Vehicle Management System Project i... x
Vehicle Management System Project in Java with Source Code | Java Projects with Source Code

1 thought on “Java Native Interface ( JNI ) Example , Calling Native C functions from JAVA”

Leave a Comment

Sign up to our newsletter!

ADB Commands Android Android Applications Android Build system Application Libraries Application Stack / User Interface Bash / Shell Scripts Bluetooth driver Cloud Technologies Commands and Packages Compilation Content Management System Core Kernel C Programs Development, Debugging and Performance Tools Development and Build Development Environment Setup Django & REST Api Errors & Failures Git Hardware Platforms HTML JAVA Programs Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL Multimedia NDK / Middleware / HAL Operating System Concepts PHP Programming Languages Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Source Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Web Design and Development Website Hosting Wordpress Yocto Embedded Linux