Part 1 of a series discussing the joys and pitfalls (mostly pitfalls) of hacking together a minimal JVM in Javascript. The live code base — a very rough work in progress! — is on GitHub. Subscribe for further updates! ;-)
This past summer, I had a conversation with a friend about his need to run small Java programs in a web page. It was a pandemic summer and I had some time, so this made me wonder how hard it would be to put together a small, simple Java Virtual Machine (JVM) in Javascript. I have done some hobby work on CPU emulation, but… I haven’t actually worked in Java or Javascript since about 2010. So ha ha ha, I was completely unqualified.
My goal was to get Hello World to run, which is to say, this bit of code:
class HelloWorld extends Object {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
If you compile and run this HelloWorld.java file in a terminal, you get this:
% javac HelloWorld.java
% java HelloWorld
Hello World
%
The first step invokes the compiler (javac
) which produces a file called HelloWorld.class. The second step executes the compiled class’s main
method.
I was interested in the second half, executing the compiled class file. I wasn’t sure…