How mikexwang/mini_jvm works
Project type and purpose
• This is a mini Java Virtual Machine implementation in C, aimed at resource-constrained devices (iOS, Android, embedded). It includes:
• A Java class library ported from CLDC1.1 (in javalib/)
• A C-based JVM engine (mini_jvm/jvm/) plus utilities (mini_jvm/utils/)
• A Swift iOS test harness (mini_jvm/iostest/)
• A minimal Java test suite (javalib_test/)
File structure
• Top-level:
• javalib/ – Java source (foundation classes) + NetBeans build artifacts
• javalib_test/ – Java test stubs + build files
• mini_jvm/ – CMake project with subfolders:
– jvm/ (core JVM source)
– utils/ (data structures, memory-leak detector, charsets, containers)
– iostest/ (Xcode Swift app for runtime verification)
• Configuration/build:
• javalib/build.xml – Ant build for Java classes
• mini_jvm/CMakeLists.txt – CMake for native code
• .gitignore/.gitattributes
README for insights
• Supports Java class file versions 5–8, threads (pthread), GC, JDWP remote debug, native methods, I/O, networking
• Build instructions:
• Java classes via NetBeans/Ant → output to javalib/build/classes/
• Native engine via CLion/CMake → mini_jvm executable/library
• iOS via Xcode Swift project linking mini_jvm
Main components
a. Java Class Library (javalib)
– Packages: java.*, javax.*, com.sun.cldc.*
b. Java Test Suite (javalib_test)
– Minimal stubs for test classes
c. JVM Engine (mini_jvm/jvm)
– Class Loader (class_load.c)
– Bytecode Interpreter (interpreter.c)
– Garbage Collector (garbage.c)
– Runtime support (runtime.c, global.c)
– Native method bridge (java_native_*.c)
– Debug interface (jdwp.c)
– Platform abstraction (os_linux.c, os_win.c)
d. Utility Library (mini_jvm/utils)
– Data containers: arraylist, hashtable, hashset, linkedlist, spinlock
– UTF-8 string handling
– Memory-leak detector (cmem)
– Compression/miniz
e. iOS Swift Test App (mini_jvm/iostest)
– Swift wrapper that embeds or calls the mini_jvm library
– UI layer (ViewController) to load/run a Java class
f. Build/Toolchain
– Java: NetBeans/Ant, JDK 1.8
– C: CMake, Clang/GCC, pthread
– Swift: Xcode, LLVM 9
Relationships and data flows
• Build pipeline
• Java sources → compiled to .class files → packaged under javalib/build/classes/
• C sources → compiled by CMake → produces mini_jvm binary (CLI or library)
• Swift project links against mini_jvm as a static/dynamic library
• Runtime flow
1. Launcher (CLI or Swift) invokes the JVM engine
2. Class Loader reads .class files from classpath
3. Bytecode Interpreter executes methods, invokes native bridges when needed
4. Garbage Collector reclaims memory periodically
5. JDWP Module opens a debug socket (port 8000) for remote debugger clients
6. Thread Scheduler maps Java threads to OS threads (pthreads)
7. I/O & Network calls flow through java_native_io.c → OS APIs
Architectural patterns & design principles
• Modular separation: class library vs. VM engine vs. utilities vs. test harness
• Layered design:
• Presentation (CLI or Swift UI)
• VM Runtime (loader → interpreter → GC → JNI)
• Native OS abstraction (pthread, socket, file system)
• Plug-in/debug port: JDWP layer sits between interpreter and network stack
• Reusability: utilities folder is shared code for containers and memory checks
• Clear component names and boundaries
• External interfaces (OS threads, file system, network)
• Build tools and outputs as ancillary artifacts