GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - TheDevAtlas/ChaiDev: Java to Rust converter

BranchesTags
Open more actions menu

Latest commit

 

History

8 Commits
8 Commits

Folders and files

NameName
Last commit message
Last commit date
.mvn
.mvn
 
 
.vscode
.vscode
 
 
examples/input
examples/input
 
 
output
output
 
 
src/main
src/main
 
 
target
target
 
 
.gitattributes
.gitattributes
 
 
.gitignore
.gitignore
 
 
ARCHITECTURE.md
ARCHITECTURE.md
 
 
LICENSE
LICENSE
 
 
QUICKSTART.md
QUICKSTART.md
 
 
README.md
README.md
 
 
mvnw.cmd
mvnw.cmd
 
 
pom.xml
pom.xml
 
 
run-converter.ps1
run-converter.ps1
 
 
run-converter.sh
run-converter.sh
 
 

Repository files navigation

Java to Rust Converter with JNI Bridge

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.

Features

  • šŸ”„ 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

Architecture

Conversion Strategy

The converter uses a hybrid approach:

  1. Pure Rust Conversion: Basic Java primitives, control flow, and data structures are converted to idiomatic Rust
  2. JNI Bridges: Java standard library classes (java., javax., org.*, etc.) use JNI wrappers to maintain compatibility
  3. Logic Preservation: Prioritizes maintaining functional logic over achieving 100% pure Rust

Why JNI?

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

Prerequisites

For Building the Converter

  • Java 11 or higher
  • Maven 3.6+

For Using the Converter

  • Java Development Kit (JDK) 11+
  • Rust toolchain (rustc, cargo)
  • JAVA_HOME environment variable set

Installation

Build from Source

# 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

Usage

Basic Usage

java -jar target/java-to-rust-converter-1.0.0-jar-with-dependencies.jar <input-dir> <output-dir>

With Clean Output

java -jar target/java-to-rust-converter-1.0.0-jar-with-dependencies.jar <input-dir> <output-dir> --clean

Arguments

  • <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

Example

# 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

Project Structure

Input (Java Project)

my-java-project/
ā”œā”€ā”€ src/
│   └── main/
│       ā”œā”€ā”€ java/
│       │   └── com/
│       │       └── example/
│       │           ā”œā”€ā”€ MyClass.java
│       │           └── Utils.java
│       └── resources/
│           ā”œā”€ā”€ config.properties
│           └── images/
│               └── logo.png

Output (Rust Project)

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

Type Mapping

Primitive Types

Java Type Rust Type
byte i8
short i16
int i32
long i64
float f32
double f64
boolean bool
char char
void ()

Common Types

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

Java standard library types (java., javax.) are wrapped in JNI:

// Java: import java.util.Date;
// Rust: JniWrapper<java.util.Date>

Example Conversion

Input (Java)

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;
    }
}

Output (Rust)

// 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()
    }
}

JNI Bridge

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",
    &[]
)?;

Configuration

Environment Variables

  • JAVA_HOME: Required for JNI compilation (path to JDK)

Cargo.toml Dependencies

The generated project includes:

[dependencies]
jni = "0.21"
jni-sys = "0.3"

Build Configuration

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");
    }
}

Limitations

  1. Method Body Conversion: Currently generates TODO placeholders for method bodies - requires manual implementation
  2. Complex Generics: Advanced generic patterns may need manual adjustment
  3. Reflection: Java reflection code needs significant reworking in Rust
  4. Annotations: Java annotations are partially supported
  5. Exception Handling: Java exceptions need to be mapped to Rust Result types

Roadmap

  • Enhanced method body conversion (expression translation)
  • Better generic type handling
  • Annotation processing
  • Exception mapping
  • Maven/Gradle dependency analysis
  • Interactive conversion mode
  • Incremental conversion support

Contributing

Contributions are welcome! Please feel free to submit issues or pull requests.

License

See LICENSE file for details.

Credits

  • ANTLR: Parser generation framework
  • jni-rs: Rust JNI bindings
  • Apache Commons IO: File utilities

Support

For issues and questions:

  • GitHub Issues: https://github.com/TheDevAtlas/ChaiDev/issues

Example Projects

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.

About

Java to Rust converter

Resources

Readme
MIT license
Activity

Stars

0 stars

Watchers

0 watching

Forks

1 fork
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800Ɨ600. No cookies required.