Sub topic 14
Generating bindings with jextract
This chapter assumes you can already build a downcall handle by hand, as in the earlier chapters. It also assumes you know FunctionDescriptor, MemoryLayout and Arena, and that you have JDK 22 or later.
The problem jextract solves
Writing a FunctionDescriptor, a MethodHandle and a struct layout for each C function is repetitive. It is also easy to get wrong. A wrong size or a wrong field order gives no compile error. It gives crashes or silent data corruption.
jextract reads a C header and generates Java source that does this work for you. It is a separate download from the JDK (from the OpenJDK jextract project page). Pick a build that matches your JDK version, because the generated code uses the java.lang.foreign API of that release.
What the tool generates
Given a header, jextract produces:
- A header class with one static method per C function. Each method wraps a downcall handle that was built from the declared signature.
- Constants for macros and enums.
- One class per struct or union, with a
layout()method and accessors for each field.
The generated code calls the same API you wrote by hand. It adds no runtime library.
Worked example: strlen from the C library
This command generates bindings for only strlen, taken from string.h:
jextract --output src/main/java \
--target-package com.example.libc \
--header-class-name LibC \
--include-function strlen \
string.hThe options mean the following:
--outputsets the source root where files are written.--target-packagesets the Java package of the generated classes.--header-class-namenames the class that holds the functions.--include-functionrestricts the output to the named function. Without a filter, jextract generates code for everything in the header, which can be thousands of declarations.
Now call the generated method. The code below is written for Java 22. On Java 23 and later, allocateUtf8String is called allocateFrom.
import com.example.libc.LibC;
import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;
public class GeneratedStrlen {
public static void main(String[] args) {
try (Arena arena = Arena.ofConfined()) {
MemorySegment str = arena.allocateUtf8String("Hello, jextract");
long length = LibC.strlen(str);
System.out.println(length); // 15
}
}
}Compare this with the hand-written StrlenDemo from the downcall chapter. There is no Linker, no descriptor and no invokeExact cast. The generated method already has the right Java types. The memory is still yours to manage, so you still choose the arena.
Using your own library
For a library that is not part of the C runtime, add the library name and the include path:
jextract --output src/main/java --target-package com.example.point \
-I include -l point include/point.h-l point makes the generated class load libpoint (or point.dll) when it initializes. The library must be findable at run time, for example through java.library.path.
For a struct Point with fields x and y, jextract generates these members:
| Member | Purpose |
|---|---|
| `Point.layout()` | The `StructLayout` for the struct |
| `Point.allocate(arena)` | A new segment of the right size |
| `Point.x(segment)` | Reads field `x` |
| `Point.x(segment, value)` | Writes field `x` |
Practical advice
- Regenerate the bindings when the header changes, and commit them or generate them in your build.
- Filter with options such as
--include-functionand--include-structto keep the output small. - Do not edit generated files. Change the options and run the tool again.
- Generated code still calls restricted methods, so you still run with
--enable-native-access. - Check generated type mappings on your platform, especially for
longandsize_t.
Exercise
You run jextract on a large system header and get hundreds of generated files. Name one option that reduces the output and say what else you must still do to run the generated code on Java 22.
Show answer
Use a filter such as --include-function strlen (or --include-struct) so only the declarations you need are generated. At run time you must still enable native access, for example with --enable-native-access=ALL-UNNAMED, and make sure the native library can be found.