Master the fundamental concepts of jvm internals through this focused micro-challenge.
Java bytecode lives in a structured binary format specified in the JVM spec chapter 4. Parsing it manually teaches you what \`javap\` reads under the hood.
After the magic \`0xCAFEBABE\` and version fields:
The constant pool is the spine. Opcodes like \`ldc #5\` index into it; \`invokevirtual\` references a method ref entry pairing class and name.
For example, \`Hello.class\` might pool \`"Hello"\`, \`"java/lang/Object"\`, and \`Code\` bytes for \`main\`.
\`\`\` magic: CA FE BA BE version: minor, major constant_pool_count: ... \`\`\`
Constant pool indices are one-based in the spec but easy to mishandle in parsers. Validate every index against pool length before dereferencing or you will read garbage UTF-8 strings silently.
This exercise asks you to parse or document \`.class\` layout field by field. You will read magic, version, and constant pool entries from raw bytes the way a minimal class loader would.
You will use the same mental model here when reading production interpreter source later in the track. Sketch one concrete input on paper, predict the outcome, then confirm with code. That discipline catches logic errors early and makes debugging far faster when you extend the implementation in follow-on tasks.
Parse a Java .class file from its raw bytes, as javap -v does:
All multi-byte values are big-endian.
Hex bytes (2 digits each, whitespace-separated). A token starting with # comments out the rest of its line. The token next separates several class files, whose outputs are separated by a line ---.
cLoading…
Constant pool tags:
cLoading…
%5s = %-18s with #N and the tag name, then the value.
l suffix.#i. NameAndType prints #n:#d. The refs print #c.#nt. // TEXT: the class or string text, name:desc (a name starting with < is quoted, as in "<init>":()V), or class.name:desc.none. NAME DESC FLAGS, plus attributes: Name(len), … if there are any.warning: N trailing bytes if bytes remain after the class attributes, then parsed N bytes.Parsing stops with one line (what was printed before it stays):
error: bad magic XXXXXXXX (expected CAFEBABE)error: truncated at offset N (the offset of the read that ran out)error: unknown constant tag T at #i (offset N)error: constant #i refers to #j, which is not Utf8|a Class|a NameAndTypeerror: empty inputbad byte: X (the whole file is rejected)Pool references are checked while printing the pool. Member references report #0 as the referrer.
Input:
cLoading…
Output:
cLoading…
u1/u2/u4 helpers, so a malformed file can never read out of bounds.Hidden tests cover an interface with an abstract method, Java 1.4 and Java 21 versions, trailing bytes, an unknown tag, a dangling reference, truncation inside a method, bad magic, empty input and a bad byte.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it works