Use Java 22 JEP 454: Foreign Function & Memory API to call Rust functions by FFI.
- JDK 22: https://jdk.java.net/22/
- GraalVM 22: https://www.graalvm.org/
- jextract: https://github.com/openjdk/jextract
- Rust: https://rustup.rs/
src/main/rust
: Rust library cdylib source code with FFIsrc/main/java
: Java source code to call Rust functions by FFIRustLibWrapper.java
: Java wrapper class to call stub code.
- Write Rust functions with
#[no_mangle]
andextern "C"
attributes. - Build Rust functions as a shared library with
cdylib
crate type. - Generate lib.h header file with
cbindgen
tool for Rust FFI functions. - Use
jextract
tool to generate Java bindings from the header file and shared library. - Write Java code to call the stub functions generated by
jextract
.
Please refer Foreign Function and Memory API in Native Image
$ git clone https://github.com/openjdk/jextract.git
$ cd jextract
$ ./gradlew -Pjdk22_home=$HOME/.jbang/cache/jdks/22 -Pllvm_home=/Library/Developer/CommandLineTools/usr/ clean verify
$ cp -r build/jextract ~/tools/jextract
- cbindgen: https://github.com/mozilla/cbindgen
- jextract: https://github.com/openjdk/jextract
- Welcome, GraalVM for JDK 22! https://medium.com/graalvm/welcome-graalvm-for-jdk-22-8a48849f054c
- Java Foreign Function Interface with RocksDB: https://rocksdb.org/blog/2024/02/20/foreign-function-interface.html