| Crates.io | rusty-jvm |
| lib.rs | rusty-jvm |
| version | 0.4.0 |
| created_at | 2025-05-26 18:17:50.988592+00 |
| updated_at | 2025-08-20 20:31:04.087689+00 |
| description | An implementation of a Java Virtual Machine (JVM). |
| homepage | https://github.com/hextriclosan/rusty-jvm |
| repository | https://github.com/hextriclosan/rusty-jvm |
| max_upload_size | |
| id | 1690109 |
| size | 535,891 |
This project is a Java Virtual Machine (JVM) implemented in Rust, built to run Java programs independently of existing JVMs. Everything related to Java is implemented from scratch. The current version executes Java bytecode in interpreted mode, with the introduction of a Just-In-Time (JIT) compiler identified as a future milestone. The next major objectives include the integration of garbage collection and support for multithreading.
See integration tests for broader examples of supported Java features.
This project relies on standard library classes from the OpenJDK JDK 23 General Availability Release.
To run the Java code, you must have JDK 23 installed on your machine and ensure the JAVA_HOME environment variable is properly set.
Ensure the following are set up:
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 samples.patterns.composite;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class CompositePattern {
public static void main(String[] args) {
Unit eliteSquad = new UnitGroup(
new Assassin(50),
new Assassin(55));
Unit namelessSquad = new UnitGroup(
() -> 10,
new Soldier("Soldier", "For honor!", 2));
Unit army = new UnitGroup(eliteSquad, namelessSquad);
System.out.println("Army attack power is " + army.damage());
}
}
interface Unit {
int damage();
}
interface TalkingUnit extends Unit {
String name();
String phrase();
int damageValue();
@Override
default int damage() {
int dmg = damageValue();
speak(dmg);
return dmg;
}
default void speak(int dmg) {
System.out.println(name() + "(" + dmg + ") says: " + phrase());
}
}
abstract class AbstractTalkingUnit implements TalkingUnit {
private final String name;
private final String phrase;
private final int baseDamage;
protected AbstractTalkingUnit(String name, String phrase, int baseDamage) {
this.name = name != null ? name : getClass().getSimpleName();
this.phrase = phrase != null ? phrase : "Arrr!";
this.baseDamage = baseDamage;
}
@Override
public int damageValue() {
return baseDamage * damageMultiplier();
}
@Override
public String phrase() {
return phrase;
}
@Override
public String name() {
return name;
}
protected abstract int damageMultiplier();
}
class Assassin extends AbstractTalkingUnit {
public Assassin(int damageValue) {
super(null, "Target acquired.", damageValue);
}
@Override
protected int damageMultiplier() {
return 2;
}
}
class UnitGroup implements Unit {
private final List<Unit> units = new ArrayList<>();
public UnitGroup(Unit... units) {
this.units.addAll(Arrays.asList(units));
}
@Override
public int damage() {
return units.stream()
.mapToInt(Unit::damage)
.sum();
}
}
record Soldier(String name, String phrase, int damageValue) implements TalkingUnit {
}
Compile the program using the Java compiler:
javac -d . CompositePattern.java
Run it using rusty-jvm:
cargo run -- samples.patterns.composite.CompositePattern
If everything is set up correctly, you should see:
Assassin(100) says: Target acquired.
Assassin(110) says: Target acquired.
Soldier(2) says: For honor!
Army attack power is 222
rusty-jvm is licensed under the MIT License.