This Java library provides serial port access for Linux, macOS, and Windows using Java's Foreign Function & Memory (FFM) API
(java.lang.foreign).
serial-ffm requires Java 23 (java.base) or newer.
(Older versions exist for JDK 19, JDK 21, and JDK 22).
The implementation is a port of the serial-native C libraries written to use JNI.
The Java bindings from native library headers are generated using jextract, leveraging the gradle-jextract plugin in the gradle build file.
./gradlew build -x test
Jextract is not run by default; to run it, pass -PrunJextract.
Using a serial port named portId in a try-with-resources statement:
try (var port = SerialPort.open(portId)
.baudrate(19_200)
.databits(8)
.parity(Parity.Even)
.stopbits(StopBits.One)
.flowControl(FlowControl.None)) {
var in = port.inputStream();
var out = port.outputStream();
// use streams ...
}