rusty-jvm 0.2.1

An implementation of a Java Virtual Machine (JVM).
Documentation

rusty-jvm

Platform Crate Docs Build Status License codecov dependency status

Introduction

Writing a JVM has long been a dream of mine, so I decided to give it a try combining my curiosity about how suitable Rust is for such a task with my desire to see how far I could push it. I'm not the first to explore this idea this project is a well-known and easily searchable example but unlike that project, I aim to create a JVM capable of running as much Java code as possible. I have to say, I didn’t expect to get this far. Java code is run in interpreted mode, meaning the JVM reads and executes bytecode instructions directly without compiling them to native code, so please don't expect high performance. There is no dependency on any existing JVM implementation. Everything related to Java is implemented from scratch. One major feature that’s still missing is garbage collection. That’s the next big milestone.

Implemented Key Features

See integration tests for broader examples of supported Java features.

Java Standard Library Classes

This project uses Java standard library classes based on OpenJDK JDK 23 General-Availability Release. The source code and class files are located in the lib directory of this repository.
They are distributed under the GNU GPLv2 with Classpath Exception. OpenJDK produces OS-specific distributions of these classes.
For simplicity, this project includes modified (under GPLv2+CE) versions usable across Windows, macOS, and Linux. You can still use original OpenJDK classes, but ensure they're compiled for the OS on which you're running rusty-jvm.

Getting Started

Prerequisites

Ensure the following are set up:

  • A machine running Windows, macOS, or Linux
  • Rust installed and configured
  • The Java standard library classes, required via one of the following:
    • Run the installation command: rusty-jvm --install
    • OR set the RUSTY_LIB_DIR environment variable to the path of the standard library
      (recommended: the lib directory in this repository)

Example Program

This Java program calculates the total attack power of a group of game units.
It uses abstract classes, interfaces, and polymorphism to showcase rusty-jvm's capabilities.

package game;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Demo {
    public static void main(String[] args) {
        ControlGroup controlGroup = new ControlGroup();
        controlGroup.addUnits(new Zealot(), new DarkTemplar(), new Unit() {
            @Override
            public int damage() {
                return 4;
            }
        });

        int groupAttackPower = controlGroup.damage();
        System.out.print("Group attack power is ");
        System.out.println(groupAttackPower);
    }
}

interface Unit {
    int damage();
}

abstract class AbstractUnit implements Unit {
    public AbstractUnit() {
        System.out.println(say());
    }
    protected abstract String say();
}

class Zealot extends AbstractUnit {
    @Override
    public int damage() {
        return 8;
    }
    @Override
    public String say() {
        return "We embrace the glory of battle!";
    }
}

class DarkTemplar extends AbstractUnit {
    @Override
    public int damage() {
        return 45;
    }
    @Override
    public String say() {
        return "Battle is upon us!";
    }
}

class ControlGroup implements Unit {
    private final List<Unit> units = new ArrayList<>();
    public void addUnits(Unit... units) {
        this.units.addAll(Arrays.asList(units));
    }
    @Override
    public int damage() {
        int totalAttackPower = 0;
        for (Unit unit : units) {
            totalAttackPower += unit.damage();
        }
        return totalAttackPower;
    }
}

Steps to Run

  1. Compile the program using the Java compiler:

    javac -d . Demo.java
    
  2. Run it using rusty-jvm:

    cargo run -- game.Demo
    

Expected Output

If everything is set up correctly, you should see:

We embrace the glory of battle!
Battle is upon us!
Group attack power is 57

License

rusty-jvm is licensed under the MIT License.

Standard library classes in lib directory are distributed under the GNU GPLv2 with Classpath Exception.