Did you ever want to access native code in pure Java?
Well now you can with Java’s Foreign Function & Memory APIs in Project Panama!
Foreign Function & Memory APIs
Since the early days of Java (1.1) developers used JNI (Java Native Interface) to access native libraries, however it required added native wrapper code to be created. Fast forward to today, Project Panama and its Foreign Function and Memory APIs drop this requirement and will perform better or comparable with pure Java code.
In a series of in-depth articles over at Foojay.io you will learn the fundamentals on how to access native libraries using Project Panama and without resorting to JNI (Java Native Interface) or JNA (Java Native Access). These articles will help you get up to speed on what is new in OpenJDK 19+ and beyond that will give your applications a competitive advantage.
A summary of the four-part series is shown below.
Part 1: Java Enhancement Proposals
JEP 424 is an umbrella project that makes up many JEPs. This proposal is broken out into two main categories with a focus on the Foreign-Memory Access API and Foreign Function API.
Before getting into the code, you will learn about past strategies and the advantages of using foreign function and memory APIs instead of JNI (Java Native Interface). Next, you will get to know a new JDK tool called jextract which can generate Java code targeting native libraries. Later you will create a HelloWorld example that will show how to access the C language’s standard input/output APIs.
After getting your feet wet with a HelloWorld example you will learn the basics of creating C language primitives datatypes and progressing to creating arrays. What is nice about creating variables or array structure is that memory or value layouts can be created off the Java’s memory heap.
Part 2: Basic C Language Fundamentals
In Part 2 You will learn about basic C language fundamentals such as C Pointers, Structs and Array of Structs. By learning the basics of the C language, you will be capable of accessing any C library using Java’s foreign function APIs. The primary focus of Part 2 is to understand how Java’s VarHandle (Since Java 7) is used to interrogate memory and value layouts off heap in a standard way.
Part 3: Accessing C Standard Library Functions
In Part 3 you will home in on your newly found skills of accessing C standard library functions. Here you will learn how to access popular 3rd party libraries. During Part 3 of my blog series, I teamed up with Brice Dutheil (@BriceDutheil). Brice contributed immensely to the SDL & OpenGL Example.
This article has a fun example of using the popular SDL and OpenGL library animating a 3d surface. Lastly, I will talk about how looking at low-level code jextract generates to better understand symbol lookup and native functions. Learning this allows you to create your own bindings (MethodHandles) to native functions.
Part 4: Creating Callback Functions
The final Part 4 of the series is how to create callback functions using the foreign function API’s upcall facility. Before in Part 1-3 I have only discussed Java code talking to native code, but what about going in the opposite direction? In other words, native code talking to Java code.
Project Panama References
- OpenJDK EA builds including jextract – https://jdk.java.net/panama/
- GitHub openjdk/jextract project – https://github.com/openjdk/jextract
- Project Panama Examples: (Project Panama for Newbies) https://github.com/carldea/panama4newbies
- JNA (Java Native Access): https://github.com/java-native-access/jna
- JEP 424 Preview – https://openjdk.java.net/jeps/424
- JDK 18 Release date & notes: https://jdk.java.net/18/release-notes
- Fosdem 2022 Java Panama talk: Native Language Access: Project Panama for Newbies (Carl Dea)