Expand description

Transfer function calls between Rust and Java in a rust-like (ish) way.


  • Call static java functions from rust.
  • Call associated rust functions from java.
  • Convert java objects to rust structs.


  • Can not call java methods from rust.
  • Can not call rust methods from java.
  • Can only convert the following rust types to java objects:
    • String
    • i8
    • i16
    • i32
    • i64
    • f32
    • f64


// Rust
use javawithrust::prelude::*;
pub struct CustomRustStruct;
impl CustomRustStruct {
    fn hello() { // Will return `Result<(), String>`
        return Ok(());
    // Will call `io.example.CustomJavaClass.world()`.
    fn world();
    // Callable from java.
    fn sum(a : i32, b : i32) -> i32 { // Will return `Result<i32, String>`
        return Ok(a + b);
// Java
package io.example;
import org.astonbitecode.j4rs.api.Instance;
import org.astonbitecode.j4rs.api.java2rust.Java2RustUtils;
public class CustomJavaClass
    // Load the dynamic library.
    // The library is loaded from the same directory as the jar.
    static {
        String os=System.getProperty("os.name").toLowerCase();String path=J2RS.class.getProtectionDomain().getCodeSource().getLocation().getPath();path=path.substring(0,path.lastIndexOf("/")).replaceAll("%20"," ")+"/"+
        "robot"; // Name of the dynamic library file. `.so` and `.dll` are added automatically.
        if (os.contains("linux") || os.contains("unix") || os.contains("android")) {
            path += ".so";
        } else if (os.contains("win")) {
            path += ".dll";
        else {throw new UnsatisfiedLinkError("Can not load dynamic library in unknown operating system `" + os + "`");}
    public static native void hello();
    public static void world() {
    private static native Instance<Integer> sum(Instance<Integer> i1, Instance<Integer> i2);
    public static Integer sumFromRust(Integer a, Integer b) {
        // Convert the objects.
        return Java2RustUtils.getObjectCasted(J2RS.sum(


Pulls in all required objects and types.

Attribute Macros

Define a Java class.
Define static Java class functions to bind.