Introduction 👋
Welcome to java-bindgen, an easy-to-use Java JNI (Java Native Interface) bindings generator and CLI tool for building Java JARs. This tool simplifies the process of integrating Rust and Java, enabling you to leverage Rust's performance and safety in your Java applications.
Goal 🚀
Develop a robust and safe framework that facilitates seamless and secure integration between Java and Rust using JNI, minimizing the complexity and risks associated with native code interoperability.
Features 🎖️
- Convenient error handling using
JResult<T, JException>
with propagation to the Java layer. - Automatic type conversion for Java primitives like
String
,byte[]
,int
,long
,float
,boolean
, etc. - Custom types with
#[derive(JavaClass)]
for seamless integration. - Integrated Logger
#[derive(JLogger)]
for better debugging and logging support. - Rust error
stack trace
attached to Java Exceptions for improved error diagnostics. - Support for Java
java.util.List<E>
with RustJList<E>
. - Support for Java nullable types in Rust using
Option<T>
.
Prerequisites
Install Rust and Cargo:
Rust Project Setup 🦀
Install java-pack
CLI 🛠️
Example:
Add java-bindgen
dependency
Add Cargo.toml
configuration:
[]
= "your.java.package"
Set crate-type
:
[]
= ["cdylib"]
Verify Your Configuration 🔦
To confirm your setup, run the following command:
Example
☢️ The following examples do not compile due to missing configurations in the Cargo.toml file. ☢️
lib.rs
use *;
Building jar 🫙
Produces the following Java interface and User class:
public static native User ;
Testing 💯
Create Java test project:
Add Test:
public void
Run tests:
Safety 🛡️
Although this crate forbids unsafe
code, the underlying JNI
(Java Native Interface) itself is not inherently safe. Therefore, thorough testing is required to ensure that your software is safe to run.
🚨 Any Rust panic that is not handled on the Rust side will cause the JVM to crash. 🚨
Project 📦
Project structure 📌
java-bindgen
- main cratejava-bindgen-macro
- macro systemjava-bindgen-core
- shared libjava-pack
- building tool
Project status 🚧
Project is in early state of development. Each release is prior tested but api changes will most likely to happen in the future as the project progress.
Roadmap 📆
To be determined. If you like the project, please consider giving it a ⭐, filing an issue ❗, or submitting a pull request (PR) ✅. Your feedback and contributions are highly appreciated!
Alpha ❗
This crate was developed and tested on Linux so more tests are needed to ensure that it works on all platforms. Multiplatform jar support allso needs more testing.
More Examples 🤖
Logger
lib.rs
use *;
;
output
Exception Handling
Rust
Java signature:
String
When Java pass non String Object
Complex Types
Rust
Java
Node parent ;
Node child ;
Element element ;
Element updated ;
System.out.;
output
) )
Full Examples 🧭
For full examples visit: github.com/java-bindgen/examples
Acknowledgments 💌
This crate strongly relies on the jni crate. Without it, this project would not have been possible. A big Thank you
to the jni crate team for their hard work and dedication!