Speaking at GeeCON 2025: A Memorable Kraków Experience

I had the pleasure of attending GeeCON 2025 in Kraków—my very first time at the conference. While the sessions were excellent, what truly stood out was the strong sense of community that made the experience special. I was also lucky to have some great conversations beyond the tech. I had a wonderful discussion with Shaaf, … Read more

A Journey to DevNexus: Challenges, Friendships, and Java

I love participating in conferences—you get to meet so many industry and thought leaders in person, hang out with them, and build lasting bonds. That’s why I had always wanted to attend DevNexus, which isn’t far from where I live. Despite receiving an invitation, my trip kept getting delayed due to visa complications. But finally, … Read more

JetBrains Junie: My Firsthand Experience

LLMs are taking over the world—almost literally. Every day, we hear about new models popping up, pushing the boundaries of what AI can do. While some worry about it, others welcome it with open arms. The truth is that AI will inevitably take over certain tasks, much like cars replaced by horse-drawn carriages. But that … Read more

DIY JVM Part 1: Decoding the Magic – Parsing Java

It’s been about 15 years since I last touched C programming. Back in university, I remember writing a small C program for an assignment—just a simple parser that could scan a C source file and check if its structures were correctly formed. It was nothing fancy, just a mundane academic exercise, and to be honest, … Read more

Exploring New Features in JDK 23: Simplifying Java with Primitive Type Patterns with JEP 455

Java continues to evolve, introducing features that streamline coding practices and improve readability. JEP 455 is one such proposal that enhances the switch statement, making it more versatile and expressive. This article delves into how JEP 455 can be utilized to handle complex decision-making scenarios more efficiently. We’ll examine a practical example to illustrate the … Read more

Accessing Native C Functions from Java Using OpenJDK’s JEP 454: Foreign Function & Memory API

Introduction

Java’s robustness and cross-platform capabilities have made it a staple in enterprise applications. However, there are scenarios where Java applications need to interact with native libraries written in languages like C or C++. The Java Native Interface (JNI) has been the traditional solution for such needs, but it comes with its own complexities and performance overheads. OpenJDK’s JEP 454, aims to offer a more straightforward and efficient alternative. Part of Project Panama, this API is designed to improve Java’s capabilities for interacting with native code. In this article, we’ll walk through a simple example to demonstrate how to use JEP 454 to call a C function from a Java program.

It’s important to note that this JEP is still in preview mode. To experiment with it, you’ll need JDK 22. You can use SDKMan to install JDK 22 on your system easily.

What is in JEP 454?

Java Enhancement-Proposal (JEP) 454 aims to introduce a Foreign Function & Memory API that simplifies the interaction between Java and native code. The API consists of two main components:

  1. Foreign Function Interface (FFI): Enables Java programs to call native functions easily, abstracting much of the boilerplate code required in JNI.
  2. Memory Access API: Provides a set of tools for interacting with native memory, including features for memory allocation, deallocation, and manipulation of native data structures.

JEP 454 is designed to be highly performant and includes various safety checks to prevent common pitfalls like buffer overflows. It is intended to replace JNI for most use cases, offering a more efficient and safer way to access native libraries and manage native memory.

Creating the C Library

First, let’s create a C program that contains a simple function to add two integers. The code for this is as follows:

#include <stdio.h>

int add(int a, int b) {
    return a + b;
}

After writing the code, save it in a file named addition.c. To compile this into a shared library, navigate to the directory where the file is located and run the following command:

gcc -shared -o libaddition.so -fPIC addition.c

This command compiles the C code into a shared library named libaddition.so, ready to be accessed by our Java program.

Writing the Java Program

Next, let’s write a Java program that uses the Foreign Function & Memory API to call the add function from our C library. The Java code is as follows:

import java.lang.foreign.*;
import java.nio.file.Path;

public class Main {
    void main() {

        try (var arena = Arena.ofConfined()) {
            var lib = SymbolLookup.libraryLookup(Path.of("libaddition.so"), arena);
            var linker = Linker.nativeLinker();
            var fd = FunctionDescriptor.of(ValueLayout.JAVA_INT, ValueLayout.JAVA_INT, ValueLayout.JAVA_INT);
            var addFunc = lib.find("add").get();

            var methodHandle = linker.downcallHandle(addFunc, fd);
            var sum = methodHandle.invoke(1, 2);
            System.out.println("sum = " + sum);
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }
}

Replace "libaddition.so" with the actual path to the compiled C library.

Running the Java Program

To compile and run the Java program, use the following command:

java --source 22 --enable-preview Main.java

If everything is set up correctly, the output should display:

sum = 3

Conclusion

OpenJDK’s JEP 454 offers a promising alternative to JNI for interacting with native code. By providing a simpler, safer, and more efficient API, it has the potential to revolutionize how Java developers work with native libraries and memory.

Sealed Interfaces and Pattern Matching: A Quick Dive into Java’s Modern Capabilities

Sealed classes and interfaces, together with pattern matching, provide powerful new tools for more explicit, controlled, and flexible design in Java

Preparing for JDK 21: A Comprehensive Overview of Key Features and Enhancements

As we inch to the release of JDK 21 in September (next month!), get familiar with the features and improvements this version will bring!

Dive into the OpenJDK: Top 10 Reads on Foojay.io

Foojay.io is a community platform dedicated to the needs of developers who use the OpenJDK and related technologies.

Charting the Course of Java: An Insightful Conversation with Java Champion, Sebastian Daschner

Explore the world of Java with Champion Sebastian Daschner, discussing his journey, future trends in Java, and tips for developer growth.