Open In App

Java Native Interface with Example

Last Updated : 11 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
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");
 
        print_Hello();
    }
}


Runnning the Program in Console:

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 "HelloJNI.h"  
 
// Implementation of the native method sayHello()
JNIEXPORT void JNICALL Java_GFG_print_1Hello(JNIEnv *, jobject) {
   printf("Hello World!\n");
   return;
}


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

Advantages of using JNI are mentioned below:

  • 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.

Conclusion

Java Native Interface is a powerful tool for integrating Java with native code, offering performance enhancements and access to platform-specific functionalities. It helps us to use Java Program with other native Languages , as it acts as a Bridge between Java and Native Languages.

Related articles:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads