Skip to content

Latest commit

 

History

History
94 lines (69 loc) · 2.88 KB

File metadata and controls

94 lines (69 loc) · 2.88 KB

Setting up libmodbus

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.

1) Install libmodbus

Ubuntu / Debian

sudo apt update
sudo apt install libmodbus-dev
# Headers:  /usr/include/modbus/modbus.h
# Library:  /usr/lib/x86_64-linux-gnu/libmodbus.so

Fedora / RHEL

sudo dnf install libmodbus libmodbus-devel
# Headers:  /usr/include/modbus/modbus.h
# Library:  /usr/lib64/libmodbus.so

macOS (Homebrew)

brew 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.dylib

Windows (MSYS2)

pacman -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.

Using this repo (mymodbus)

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.h

2) Run jextract

Key flags:

  • -I — directory containing modbus.h
  • -L — directory containing the shared library (.so / .dylib / .lib)
  • -l modbus — library name to link
  • -t — target Java package

Linux (Debian/Ubuntu x86_64)

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.jar

3) Run your Java application

At 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

4) Tips and gotchas

  • 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 on PATH.
  • Multiple headers: if your code needs additional system headers (e.g. <sys/types.h>), add more -I flags for your toolchain directories.
  • Library name: the library is usually modbus (libmodbus.so / libmodbus.dylib / modbus.dll). Adjust -l if your system uses a different name.
  • ABI: compile and use the library for the same architecture as your JVM (x64 vs aarch64).