Pava is a simple, educational Java Virtual Machine (JVM) interpreter written in Python. It aims to emulate core JVM functionality by parsing and executing Java .class
files, including loading classes, handling the constant pool, and interpreting bytecode instructions. This project was inspired by JelloVM by tsoding.
While Pava doesn’t cover the full JVM specification, it handles a variety of Java bytecode instructions, making it a valuable tool for learning about JVM internals.
├── Main.java # Sample Java main class for testing
├── README.md # Project documentation
├── java.base # Directory for built-in Java classes
│ └── .keep # Placeholder file
├── jvmconsts.py # Constants for the JVM, including opcodes and flags
├── jvmparser.py # JVM class file parser and attribute handlers
├── main.py # Main interpreter program
├── useful # Useful documentation links
│ ├── Chapter 4. The class File Format.webloc
│ └── Chapter 6. The Java Virtual Machine Instruction Set.webloc
└── utils.py # Utility functions for byte parsing
-
Clone the Repository
git clone https://github.com/username/pava cd pava
-
Requirements
- Ensure you have Python 3.x installed.
- Install dependencies if needed (e.g.,
dataclasses
,enum
,typing
,io
for more advanced features).
To execute a Java .class
file, use the following command from the repository root:
python3 main.py path/to/Main.class
Note: The interpreter requires the
.class
file format, not.java
source files. You can compile.java
files withjavac Main.java
and runpython3 main.py Main.class
.
This module contains constants for various JVM opcodes, attribute types, and access flags.
Responsible for parsing .class
files, this module reads bytecode, builds a constant pool, and handles class methods, fields, and attributes. Highlights include:
parse_class_file()
: Loads a class file, parsing the constant pool and main class structures.parse_constant_pool()
: Reads constants (e.g.,CONSTANT_Methodref
,CONSTANT_Class
).parse_attributes()
: Interprets JVM attributes likeCode
,SourceFile
, andLineNumberTable
.
The main interpreter script. It reads .class
files, executes the main method by interpreting bytecode instructions, and supports basic Java I/O operations.
Contains helper functions for parsing binary data (e.g., parse_u1
, parse_i2
) and type conversions essential for decoding the .class
file structure.
- Basic Bytecode Execution: Supports a variety of JVM instructions (e.g.,
getstatic
,invokevirtual
,ldc
,return
). - Class and Method Loading: Dynamically loads classes and methods referenced in the bytecode.
- Constant Pool Handling: Parses and manages the constant pool, enabling access to constants within a
.class
file. - Error Handling: Detects unsupported operations and provides informative runtime errors.
Pava is limited to a subset of JVM features. Advanced features like garbage collection, multi-threading, and full invokedynamic
support are not implemented.
- Enhanced Bytecode Support: Expand the opcode coverage for more complex Java programs.
- Improved Standard Library Emulation: Support additional standard Java classes and methods.
- Optimization: Refine the interpreter for better performance on complex code.