Java learning pathSection 20

JNI (Java Native Interface)

Understand the bridge between Java code and native libraries written in C/C++.

1

JNI Architecture

JNI defines a standard mechanism for Java code to call native methods and for native code to access Java objects.

It acts as the bridge between managed Java execution and host native libraries.

Java code executes within the Java runtime and calls a native C or C++ library through JNI to reach OS APIs; native callbacks return through JNI to Java methods and objects.
JNI Architecture
2

Native Methods

Native methods are declared in Java but implemented in native languages like C or C++.

These methods are used when platform capabilities or performance-critical behavior are not available in pure Java.

3

Native Libraries

Native libraries are compiled shared objects or DLLs loaded by the JVM for use through JNI.

They are commonly used for OS integration, graphics, networking, and high-performance system access.

4

JNI Lifecycle

The JNI lifecycle covers loading the library, linking native functions, and managing the Java-to-native transition safely.

The JVM ensures native code runs within a valid runtime context and handles thread and object management.

5

Calling C/C++ from Java

Java declares a native method and then provides a C/C++ implementation. The JVM maps method signatures and object references appropriately.

This allows Java programs to access system capabilities outside the standard library surface.

6

Calling Java from Native Code

Native code can invoke Java methods, access fields, and create Java objects using JNI.

This is useful for embedded systems, hybrid applications, and code that needs to interact with Java-managed objects from native layers.