Open In App

Java Native Interface with Example

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:

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




// 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:




/* 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).




// 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:

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:


Article Tags :