To use libmodbus with jextract and the FFM API (JDK 25), you need (1) the libmodbus headers and shared library installed, and (2) jextract pointed at those paths to generate Java bindings.
sudo apt update
sudo apt install libmodbus-dev
# Headers: /usr/include/modbus/modbus.h
# Library: /usr/lib/x86_64-linux-gnu/libmodbus.sosudo dnf install libmodbus libmodbus-devel
# Headers: /usr/include/modbus/modbus.h
# Library: /usr/lib64/libmodbus.sobrew install libmodbus
# Apple Silicon: /opt/homebrew/include/modbus/modbus.h, /opt/homebrew/lib/libmodbus.dylib
# Intel: /usr/local/include/modbus/modbus.h, /usr/local/lib/libmodbus.dylibpacman -S --needed mingw-w64-ucrt-x86_64-libmodbus mingw-w64-ucrt-x86_64-clang
# Headers: C:/msys64/ucrt64/include/modbus/modbus.h
# Library: C:/msys64/ucrt64/lib/libmodbus.dll.a
# Add C:\msys64\ucrt64\bin to PATH so Java can find the DLL at runtime.The mymodbus module runs jextract during every build (see mymodbus/pom.xml). It expects:
- Headers:
/usr/include/modbus/modbus.h - Include dir:
/usr/include/modbus
If your headers are elsewhere (macOS/Homebrew, Windows/MSYS2, custom installs), override:
export JEXTRACT_BIN=/path/to/jextract
mvn -pl mymodbus compile \
-Dlibmodbus.include.dir=/path/to/include/modbus \
-Dlibmodbus.header=/path/to/include/modbus/modbus.hKey flags:
-I— directory containingmodbus.h-L— directory containing the shared library (.so/.dylib/.lib)-l modbus— library name to link-t— target Java package
jextract \
-t es.omarall.modbus.nativeapi \
-l modbus \
-I /usr/include \
-L /usr/lib/x86_64-linux-gnu \
/usr/include/modbus/modbus.h \
-o modbus-jextract.jarAt runtime, ensure the native library is on the library search path (LD_LIBRARY_PATH / DYLD_LIBRARY_PATH / PATH):
# Linux
export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH
java --enable-native-access=ALL-UNNAMED --enable-preview \
-cp modbus-jextract.jar:target/classes es.omarall.App- jextract availability: some JDK 25 builds do not ship jextract. If it is not in
$JAVA_HOME/bin, install it separately and ensure Clang is onPATH. - Multiple headers: if your code needs additional system headers (e.g.
<sys/types.h>), add more-Iflags for your toolchain directories. - Library name: the library is usually
modbus(libmodbus.so / libmodbus.dylib / modbus.dll). Adjust-lif your system uses a different name. - ABI: compile and use the library for the same architecture as your JVM (x64 vs aarch64).