A way to use compiled Rust code as native functions in Java.
- Create a Java file that has
public static native
methods - Compile that file to a header file using
javac -h . ClassName.java
- Start up a Rust library project
- The main file must be
lib.rs
- In the
Cargo.toml
file, there should be a dependency forjni = "0.19.0"
and a lib section wherecrate-type = ["cdylib"]
- The functions in the Rust file must match the ones in the header file compiled earlier with the same parameters and parameter types
- The Rust code should then run like normal and output a Java result (if a return value is needed)
- Perform a
cargo build
on the project
- The main file must be
- Make the connection using a static code block in Java and grab the DLL file from the Rust project and use
System.load()
to load the DLL
Not sure how this could get used in the future. The linking becomes one of the most time consuming parts of the operation, then, calling the native method also creates a performance hinderance. I'd probably have to discover other places where this integration could be used.