A powerful tool that converts Java projects to Rust using ANTLR for parsing and JNI (Java Native Interface) for maintaining compatibility with Java libraries. This approach prioritizes LOGIC over PURE RUST - ensuring your converted code maintains compatibility with Java dependencies when needed.
- š ANTLR-based Parsing: Uses ANTLR 4 with Java 9 grammar for accurate Java code parsing
- š JNI Bridge Support: Automatically generates JNI wrappers for Java standard library usage
- š¦ Resource Preservation: Copies non-code files (images, configs, etc.) to maintain project structure
- šÆ Type Mapping: Intelligent mapping between Java and Rust types
- šļø Project Structure: Generates complete Rust project with Cargo.toml and build configuration
- š Code Generation: Converts classes, interfaces, enums, methods, and fields
The converter uses a hybrid approach:
- Pure Rust Conversion: Basic Java primitives, control flow, and data structures are converted to idiomatic Rust
- JNI Bridges: Java standard library classes (java., javax., org.*, etc.) use JNI wrappers to maintain compatibility
- Logic Preservation: Prioritizes maintaining functional logic over achieving 100% pure Rust
Converting complex Java projects to pure Rust is challenging because:
- Java has a vast standard library that doesn't have direct Rust equivalents
- Many Java features (reflection, annotations, etc.) work differently in Rust
- Business logic should remain functional during conversion
By using JNI bridges, we:
- ā Maintain compatibility with existing Java libraries
- ā Preserve complex Java functionality
- ā Enable gradual migration (you can replace JNI calls with pure Rust over time)
- ā Ensure the converted code actually works
- Java 11 or higher
- Maven 3.6+
- Java Development Kit (JDK) 11+
- Rust toolchain (rustc, cargo)
JAVA_HOMEenvironment variable set
# Clone the repository git clone https://github.com/TheDevAtlas/ChaiDev.git cd ChaiDev # Build with Maven mvn clean package # The executable JAR will be in target/ ls target/java-to-rust-converter-1.0.0-jar-with-dependencies.jar
java -jar target/java-to-rust-converter-1.0.0-jar-with-dependencies.jar <input-dir> <output-dir>
java -jar target/java-to-rust-converter-1.0.0-jar-with-dependencies.jar <input-dir> <output-dir> --clean
<input-dir>: Directory containing the Java project to convert<output-dir>: Directory where the Rust project will be generated--clean: (Optional) Clean the output directory before conversion
# Convert a Java project java -jar target/java-to-rust-converter-1.0.0-jar-with-dependencies.jar ./my-java-project ./my-rust-project --clean # Build the generated Rust project cd my-rust-project cargo build # Run the Rust project cargo run
my-java-project/
āāā src/
ā āāā main/
ā āāā java/
ā ā āāā com/
ā ā āāā example/
ā ā āāā MyClass.java
ā ā āāā Utils.java
ā āāā resources/
ā āāā config.properties
ā āāā images/
ā āāā logo.png
my-rust-project/
āāā Cargo.toml # Rust package manifest
āāā build.rs # JNI build configuration
āāā src/
ā āāā main.rs # Entry point
ā āāā my_class.rs # Converted from MyClass.java
ā āāā utils.rs # Converted from Utils.java
ā āāā jni_bridge.rs # JNI wrapper utilities
ā āāā jni_init.rs # JVM initialization
ā āāā my_class_bridge.rs # Class-specific JNI bridge
āāā resources/
āāā config.properties # Copied resource
āāā images/
āāā logo.png # Copied image
| Java Type | Rust Type |
|---|---|
byte |
i8 |
short |
i16 |
int |
i32 |
long |
i64 |
float |
f32 |
double |
f64 |
boolean |
bool |
char |
char |
void |
() |
| Java Type | Rust Type |
|---|---|
String |
String |
Integer |
i32 |
List<T> |
Vec<T> |
ArrayList<T> |
Vec<T> |
HashMap<K,V> |
HashMap<K,V> |
HashSet<T> |
HashSet<T> |
Java standard library types (java., javax.) are wrapped in JNI:
// Java: import java.util.Date; // Rust: JniWrapper<java.util.Date>
package com.example; import java.util.ArrayList; import java.util.List; public class Calculator { private int value; public Calculator(int initialValue) { this.value = initialValue; } public int add(int x) { value += x; return value; } public List<Integer> getHistory() { List<Integer> history = new ArrayList<>(); // Implementation return history; } }
// Original Java package: com.example // Generated from Java class: Calculator use std::collections::{HashMap, HashSet}; use jni::JNIEnv; use jni::objects::{JClass, JObject, JValue}; pub struct Calculator { pub value: i32, } impl Calculator { pub fn new(initial_value: i32) -> Self { Calculator { value: initial_value, } } pub fn add(&self, x: i32) -> i32 { // TODO: Convert Java method body 0 } pub fn get_history(&self) -> Vec<i32> { // Uses JNI bridge for Java List compatibility Vec::new() } }
The generated JNI bridge allows Rust to call Java code:
// Example: Calling Java method from Rust let jvm = jni_init::init_jvm().expect("Failed to initialize JVM"); let env = jvm.attach_current_thread().expect("Failed to attach thread"); let java_object = JniHelper::create_java_object( &env, "java/util/ArrayList", "()V", &[] )?;
JAVA_HOME: Required for JNI compilation (path to JDK)
The generated project includes:
[dependencies] jni = "0.21" jni-sys = "0.3"
The generated build.rs links against the JVM:
fn main() { if let Ok(java_home) = std::env::var("JAVA_HOME") { println!("cargo:rustc-link-search=native={}/lib/server", java_home); println!("cargo:rustc-link-lib=dylib=jvm"); } }
- Method Body Conversion: Currently generates TODO placeholders for method bodies - requires manual implementation
- Complex Generics: Advanced generic patterns may need manual adjustment
- Reflection: Java reflection code needs significant reworking in Rust
- Annotations: Java annotations are partially supported
- Exception Handling: Java exceptions need to be mapped to Rust Result types
- Enhanced method body conversion (expression translation)
- Better generic type handling
- Annotation processing
- Exception mapping
- Maven/Gradle dependency analysis
- Interactive conversion mode
- Incremental conversion support
Contributions are welcome! Please feel free to submit issues or pull requests.
See LICENSE file for details.
- ANTLR: Parser generation framework
- jni-rs: Rust JNI bindings
- Apache Commons IO: File utilities
For issues and questions:
- GitHub Issues: https://github.com/TheDevAtlas/ChaiDev/issues
Check the examples/ directory for sample conversions.
Note: This tool is designed to accelerate Java-to-Rust migration by handling the mechanical conversion and maintaining Java compatibility through JNI. Manual refinement of generated code is expected and encouraged for production use.