Technical Introduction to Foreign Function Interface in Java 25

Explore the Foreign Function & Memory API in Java 25, a modern alternative to JNI for safe and efficient native code integration, with key components and example usage.

What Is the FFM API?

The Foreign Function & Memory (FFM) API in Java 25 lets Java programs call native code and manage native memory safely and efficiently. It replaces the Java Native Interface (JNI), which is complex and error-prone.

The API was finalized in JDK 22 through JEP 454. It lives in the java.lang.foreign package. With it, developers can call native functions and work with off-heap memory directly, without JNI's boilerplate or its risks — memory leaks and crashes among them.

Core Components

The FFM API has three main interfaces:

  • Linker: This calls native functions from Java (downcalls) and Java code from native code (upcalls). It follows the platform-specific Application Binary Interfaces (ABIs). The CLinker class gives a platform-optimized implementation.
  • SymbolLookup: This finds the addresses of native functions or variables. It supports lookups in default libraries, or in specific ones via LibraryLookup.
  • MemorySegment: This manages contiguous memory regions, either on-heap or off-heap. Its safe access operations prevent memory corruption.

The API also uses two more tools. Arena manages the lifecycle of native memory. With try-with-resources, native memory is deallocated automatically. FunctionDescriptor defines native function signatures.

Why FFM API?

JNI needs a lot of C code and deep JVM knowledge. The FFM API avoids that. It makes native integration simpler. There is less boilerplate to write. Performance improves because context switching is kept to a minimum. Safety improves too, since memory is managed explicitly.

You might use it to reach native libraries such as RocksDB. It also suits work that talks to hardware directly. And it fits performance-critical applications, such as machine learning.

Example: Calling a C Function

Here is an example of calling the C strlen function with the FFM API:

import java.lang.foreign.*;
import static java.lang.foreign.ValueLayout.*;

public class FFIDemo {
    public static void main(String[] args) {
        try (Arena arena = Arena.ofConfined()) {
            MemorySegment str = arena.allocateUtf8String("Java 25 FFI");
            Linker linker = Linker.nativeLinker();
            SymbolLookup stdlib = linker.defaultLookup();
            MethodHandle strlen = linker.downcallHandle(
                stdlib.find("strlen").orElseThrow(),
                FunctionDescriptor.of(JAVA_LONG, ADDRESS)
            );
            long len = (long) strlen.invokeExact(str);
            System.out.println("String length: " + len);
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
}

What does this code do? First, it allocates off-heap memory for a string. Next, it looks up the strlen function and defines its signature. Then it invokes the function. Memory is managed safely with an Arena.

Benefits and Trade-offs

The FFM API is simpler, safer, and faster than JNI. It does require an understanding of native libraries and ABIs. Tools like jextract can make integration even easier. They generate Java bindings from C headers. JNI remains relevant for legacy code. Even so, the FFM API is the future of native interoperation in Java.

For more details, see the official documentation or JEP 454.