Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Supplier Interface in Java with Examples
Next article icon

Java Native Interface with Example

Last Updated : 24 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Next Article
Supplier Interface in Java with Examples

M

Mehak Kumar
Improve
Article Tags :
  • Java
  • java-interfaces
Practice Tags :
  • Java

Similar Reads

  • Supplier Interface in Java with Examples
    The Supplier Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which does not take in any argument but produces a value of type T. Hence this functional interface takes in only one gener
    1 min read
  • Java 8 | ObjIntConsumer Interface with Example
    The ObjIntConsumer Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in two arguments and produces a result. However these kind of functions don't return any value. Hence thi
    2 min read
  • LongFunction Interface in Java with Examples
    The LongFunction Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in a long-valued argument and produces a result of type R. This functional interface takes in only one gene
    1 min read
  • Map.Entry interface in Java with example
    Map.Entry interface in Java provides certain methods to access the entry in the Map. By gaining access to the entry of the Map we can easily manipulate them. Map.Entry is a generic and is defined in the java.util package. Declaration : Interface Map.Entry k -> Key V -> Value Methods: equals (O
    4 min read
  • ToIntFunction Interface in Java with Examples
    The ToIntFunction Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in an argument of type T and produces an int-valued result. This functional interface takes in only one ge
    1 min read
  • ToLongFunction Interface in Java with Examples
    The ToLongFunction Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in an argument of type T and produces a long-valued result. This functional interface takes in only one g
    1 min read
  • LongConsumer Interface in Java with Examples
    The LongConsumer Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in one long-valued argument but does not return any value. The lambda expression assigned to an object of L
    3 min read
  • ToDoubleFunction Interface in Java with Examples
    The ToDoubleFunction Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in an argument of type T and produces a double-valued result. This functional interface takes in only o
    1 min read
  • Java 8 | ObjLongConsumer Interface with Example
    The ObjLongConsumer Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in two arguments and produces a result. However these kind of functions don't return any value. Hence th
    2 min read
  • LongToIntFunction Interface in Java with Examples
    The LongToIntFunction Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in a long-valued argument and gives an int-valued result. The lambda expression assigned to an object
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences