Interpret something, and run the generated bytecode in Java?

bytecode, compiler-construction, interpreter, java, jvm

Solution

You can use java.lang.Classloader.defineClass(), which turns bytecode into a Class object. You can the call newInstance() on the resulting Class object, and off you go.

Problem

I'm writing a toy interpreter with a REPL in Java. I'd like to generate bytecode from the language and run that, instead of interpreting an AST and running that instead. Since my Java is a bit rusty, is it possible to run generated bytecode on the fly on the JVM?

Original source