This is a experimenting project on how to connect Rust and Java together using the JNI API. So that you can execute native code from Java.
This repository can be used as reference guide on how to get started.
-
Add java methods with a native identifier
-
Run
javah class-file
orjavah -classpath path-to-jar files class-name
path-to-jar could also contain multiple jars (
javah -classpath target/* Test
)class-name only contains the fully qualified class name without the
.class
extension -
Call your rust methods like the ones in the generated header file
-
Compile the rust binaries into a library
-
Load the library into Java using System.load(LIBRARY_PATH)
-
Now you can access the native methods
- Rust project:
cargo build
> output into target/ - Java
mvn install
using Maven (included in Eclipse, IntelliJ, Netbeans) - Place the library file (.dll, .so) next to the jar file java/target/
- Run the jar file
java -jar jar-file
- Compile Rust-JNI to small library files
- First steps with Rust and JNI
- Java General JNI Documentation
- Windows (.dll)
- Linux (.so)
- Mac (.so and .dylib)
- More complex examples
- Complete
JNINativeInterface
in Rust according to thejni.h file
This project is open for contributions of any kind. If you find a better example, description, [...], just make a pull request.
This project is licensed with the unlicense, so are free to do whatever you want. Including using it in your own project without credits.