Java Native Interface with Example
Interface: An interface in Java is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. Interfaces cannot contain instance fields or constructors, and they cannot specify implementation details for the methods they declare. They provide a way to achieve abstraction and multiple inheritance in Java.
Interfaces are used to specify what a class must do, but not how it does it. A class that implements an interface must provide implementations for all of the methods declared in the interface.
Syntax:
interface InterfaceName { // constant fields type CONSTANT_NAME = value; // abstract methods (methods without body) returnType methodName(parameterList); // default methods (with a default implementation) default returnType methodName(parameterList) { // method body } // static methods static returnType methodName(parameterList) { // method body } // nested types interface NestedInterfaceName { // nested interface members } }
Example on Interface
/ Define an interface named Animal interface Animal { // Abstract method (does not have a body) void makeSound(); // Default method (has a body) default void sleep() { System.out.println("Sleeping..."); } } // A class that implements the interface class Dog implements Animal { // Provide implementation for the abstract method public void makeSound() { System.out.println("Woof!"); } } public class Main { public static void main (String[] args) { Dog dog = new Dog(); dog.makeSound(); dog.sleep(); } }
Output
Woof! Sleeping...
The Java Native Interface (JNI) is a framework that allows Java code running in the Java Virtual Machine (JVM) to call and be called by native applications (programs specific to a hardware and operating system platform) and libraries written in other languages such as C or C++. JNI is a part of the Java platform, included in the standard JDK (Java Development Kit).
JNI provides several benefits:
Performance: Allows Java applications to execute native code for performance-critical sections.
Access to platform-specific features: JNI allows Java programs to use features available only on specific platforms or hardware.
Reuse of existing libraries: Enables Java applications to make use of existing native libraries.
Basic Steps to Use JNI
- Define a Native Method in Java:
Define the method in your Java code using the native keyword.
2. Generate Header File:
Use the javah tool to generate a C/C++ header file from the Java class.
3. Implement the Native Method in C/C++:
Implement the method in C/C++ using the generated header file.
4. Load the Native Library in Java:
Use System.loadLibrary to load the native library.
5. Run the Java Program:
Compile and run the Java program to invoke the native method.
Example of JNI
Step 1: Define a Native Method in Java
Create a Java class with a native method.
public class HelloWorld { // Declare a native method // This method is implemented in a native library (typically C or C++). public native void printHello(); // Static block to load the native library // The 'System.loadLibrary' method loads the specified native library. static { System.loadLibrary("HelloWorld"); } public static void main(String[] args) { // Create an instance of the HelloWorld class HelloWorld hello = new HelloWorld(); // Call the native method 'printHello' // This will execute the native code defined in the C/C++ library. hello.printHello(); } }a
Step 2: Generate the Header File
Compile the Java class and generate the C/C++ header file using the javah tool.
Running the program in console:
Javac -h . HelloWorld.java
FileName: HelloWorld.h
/* DO NOT EDIT THIS FILE - it is machine generated */ #include/* Header for class HelloWorld */ #ifndef _Included_HelloWorld #define _Included_HelloWorld #ifdef __cplusplus extern "C" { #endif /* * Class: HelloWorld * Method: printHello * Signature: ()V */ JNIEXPORT void JNICALL Java_HelloWorld_printHello (JNIEnv *, jobject); #ifdef __cplusplus } #endif #endif
Step 3: Implement the Native Method in C/C++
Create a C/C++ source file (HelloWorld.c) and implement the native method.
FileName: HelloWorld.c
#include#include #include "HelloWorld.h" // The JNIEXPORT and JNICALL macros ensure that the function is exported properly // and can be called from Java. JNIEXPORT void JNICALL Java_HelloWorld_printHello(JNIEnv *env, jobject obj) { // Print a message to the console from C code printf("Hello, World from C! "); }
Step 4: Compile the C/C++ Code
Compile the C/C++ code into a shared library.
For Windows:
gcc -I"%JAVA_HOME%include" -I"%JAVA_HOME%includewin32" -shared -o HelloWorld.dll HelloWorld.c
For Linux:
gcc -I"$JAVA_HOME/include" -I"$JAVA_HOME/include/linux" -shared -o libHelloWorld.so HelloWorld.c
Step 5: Run the Program
java HelloWorld
Output
Hello, World from C!