This is a very simple example of how a native Java library can be created and called from Rust programs.
- As Rust is relatively new in comparison to Java, there might be mature libraries that can be reused “almost as is”
- If you’re more comfortable with Java (just like me), you might code few parts of your Rust application in Java (as needed).
- I don’t know Rust that well and there are programs that would literally take me few hours in Java (vs. few days in Rust)
- There are several mature Java libraries with no direct equivalent in Rust, that I’d rather not write myself from scratch.
TL;DR
podman build -t localhost/rust-calls-java .
podman image prune --filter label=stage=rust-calls-java-builder
podman run localhost/rust-calls-java
You’ll need to have either Docker or Podman installed first. I’ll use podman
as this is what I have installed, but it’s literally the same syntax for docker
.
At the root of this repository, run the following command.
podman build -t localhost/rust-calls-java .
podman run localhost/rust-calls-java
Follow the instructions available in the projects in this git repository:
- First, build the
java-lib
project - Then, you should be able to build and run the
rust-app
project
- https://www.graalvm.org/reference-manual/native-image/ImplementingNativeMethodsInJavaWithSVM/
- https://www.rapidwright.io/docs/Call_RapidWright_From_CPP.html
- https://rust-lang.github.io/rust-bindgen/
- https://github.com/babashka/sci/tree/master/libsci
- https://github.com/borkdude/clojure-rust-graalvm
- https://medium.com/graalvm/isolates-and-compressed-references-more-flexible-and-efficient-memory-management-for-graalvm-a044cc50b67e
- https://docs.oracle.com/en/graalvm/enterprise/20/docs/reference-manual/native-image/C-API/