Java Native Interface with Example
Last Updated :
24 Mar, 2025
In the Programming World, it is often seen in the comparison of Java vs C/C++. Although the comparison doesn't make any such sense, as both the languages have their Pros and Cons, but what if we can use multiple languages in a single Program?
In this article, we will learn How to use JNI(Java Native Interface) to run multiple languages in a single Program.
What is Interface?
Like a class, interfaces in Java can have methods and variables, but the methods declared in the interface are by default abstract (only method signature, nobody). Interfaces specify what a class must do and not how. It is the blueprint of the class.
Syntax:
interface <interface_name>{
// declare constant fields
// declare methods that abstract
// by default.
}
What is JNI?
JNI stands for Java Native Interface. JNI is a framework in Java that allows users to run the Java Code and Operate with the applications and libraries written in other languages example C, C++, etc. It acts as a bridge between the Java Platform and Native Environment (Application or Libraries).
Working on Java Native Interface
The working of JNI revolves around a single concept of Native methods. These Native Methods are the methods that are present in Java Code but are implemented in any other native language. After covering a Native method in our article we can simply implement using the few steps mentioned below:
- A header file of the native language is created.
- Implement the native method in that language header file.
- Load the Libary in the Java Code and no error will be shown in the Program.
Example of Using JNI
In this example, we will write "Hello World!" Program. Where we will use C as a Native Language.
Below is the Main Java Program:
GFG.java
// Java Hello World Program to Demonstrate
// Java Native Interface
import java.io.*;
// Driver Class
class GFG {
public native void print_Hello();
// Load the native library
static {
System.loadLibrary("hello");
}
// Main Method
public static void main (String[] args) {
System.out.println("In this Program we will learn about Java Native");
// Create an instance of GFG and call the native method
GFG gfg = new GFG();
gfg.print_Hello();
}
}
Runnning the Program in Console:
The javah tool was deprecated in JDK 10 and removed in later versions. The modern way to generate the JNI header file is to use:
javac -h . GFG.java
After running the above console command it will create two Files: "GFG.class" file (Java Class File) and "GFG.h" file (Native Library).
Create a "GFG.h" file with the code mentioned below:
GFG.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class GFG */
#ifndef _Included_GFG
#define _Included_GFG
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: GFG
* Method: print_Hello
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_GFG_print_1Hello
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
Note: Don't Make any changes in the Native Library File
Create a GFG.C file which will use the Native Library file and will further create executable dynamic file(.so
, .dll
, or .dylib
depending on the platform).
GFG.c
// C Program to Use Print Hello World
#include <jni.h>
#include <stdio.h>
#include "GFG.h"
// Implementation of the native method print_Hello()
JNIEXPORT void JNICALL Java_GFG_print_1Hello(JNIEnv *env, jobject obj) {
printf("Hello World!\n");
return;
}
Compile the C Code
To compile the C code into a shared library, we need to use the appropriate compiler commands for specific platform
On windows:
gcc -shared -o hello.dll -I"%JAVA_HOME%\include" -I"%JAVA_HOME%\include\win32" GFG.c
On Linux:
gcc -shared -fpic -o libhello.so -I${JAVA_HOME}/include -I${JAVA_HOME}/include/linux GFG.c
On macOS:
gcc -shared -fpic -o libhello.dylib -I${JAVA_HOME}/include -I${JAVA_HOME}/include/darwin GFG.c
Running the Java Program
Now Run the Java Program and It will give the result running the Native method in Java Code.
Output:
In this Program we will learn about Java Native
Hello World!
Advantages of Java Native Interface
- Java Native Interface offers a way to enhance performance by allowing to create a Program written in Multiple that can be more directly and efficiently executed by the hardware.
- It provides access to platform-specific features and libraries that are not available or difficult to access from Java
- It enables the integration of Java applications with systems and libraries written in other languages, facilitating code reuse and extending the capabilities of applications.
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to
12 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read