jvmbytecode.class-filejvm-bytecode

How can I extend JVM bytecode?


I want to create a programming language that compiles to its own bytecode format and a VM that interprets it. But I want the bytecode to be compatible with JVM. I've searched for any way to insert comments to JVM bytecode so then I can parse them with my own VM but I couldn't find any. Also I tried to insert some bytes in the start of the byte array and in the end, but it produced ClassFormatException. Is there any workaround?


Solution

  • "compiles to its own bytecode format" and "compatible with JVM" are mutually exclusive requirements.

    If you want the JVM to be able to parse your classfiles, the classfiles must strictly comply with the Java Virtual Machine Specification Chapter 4. The class File Format.

    The standard way of exteding the class file format is Attributes. You may invent your own attributes and include them to one or more of the following structures: