Introduction
A .class file holds one compiled class, interface, enum, record,
annotation interface or module declaration. It carries the class’s methods as bytecode
for the Java Virtual Machine, and every name, type and constant they use. The
javac compiler of OpenJDK writes one
per class, nested and anonymous classes included, and the JVM loads each when the
program first needs it. Kotlin, Scala, Groovy, Clojure and other JVM languages write
the same format.
This is the format chapter 4 of the Java Virtual Machine Specification describes. It began with JDK 1.0.2 in 1996 (version 45.3), and every Java release since has added to it without changing what was there: new kinds of constant, new attributes, new flags and one new instruction. A reader that knows an older version can still find its way through a newer file, because every attribute says how long it is.
The pre-release versions 45.0 to 45.2 may use a different Code
attribute; files laid out that way are a separate format, the
Oak class file. See
all the class file variations.
A .class file dropped on any page of this site opens in the explorer,
which shows every structure in it, a disassembly in the style of
javap -c -v and the constant pool as a table.
Structure
A class file is one structure, read from start to end. All numbers are big-endian and unsigned unless stated. Only the first four fields have fixed offsets: the constant pool’s entries have different sizes, so everything after it has to be found by reading through.
0magicCA FE BA BE4 bytes4minor_version, major_version: 45.3 to 72.04 bytes8constant_pool_count, then the constant pool: every name, string, number and reference the class usesvaries…access_flags, this_class, super_class, interfaces8 bytes and 2 per interface…fields: a count, then each field with its attributesvaries…methods: a count, then each method with its Code attribute and othersvaries…attributes of the class, such asSourceFileandInnerClassesvaries
Almost everything after the constant pool refers into it by a two-byte index: a class’s name, a method’s descriptor, the string an instruction loads. The bytecode of each method sits in its Code attribute, and its instructions name fields, methods and classes through the same indexes.
What each version added
Everything below is an addition: a file may use a feature only from the version that introduced it, and nothing earlier changed its layout.
| Version | Release | Added |
|---|---|---|
| 45.3 | JDK 1.0.2 | The format as first specified: constant tags 1 and 3 to 12; the attributes ConstantValue, Code, Exceptions, SourceFile, LineNumberTable and LocalVariableTable |
| 45.3 | JDK 1.1 | InnerClasses, Synthetic and Deprecated attributes |
| 46.0 | J2SE 1.2 | ACC_STRICT on methods (strictfp) |
| 49.0 | J2SE 5.0 | Generics, annotations and enums: Signature, EnclosingMethod, SourceDebugExtension, LocalVariableTypeTable, the five annotation attributes; ACC_SYNTHETIC, ACC_ANNOTATION, ACC_ENUM, ACC_BRIDGE and ACC_VARARGS; ldc of a Class |
| 50.0 | Java SE 6 | StackMapTable, for verification by type checking |
| 51.0 | Java SE 7 | MethodHandle, MethodType and InvokeDynamic constants; BootstrapMethods; the invokedynamic instruction. jsr and ret are no longer allowed |
| 52.0 | Java SE 8 | Type annotations; MethodParameters; non-abstract, private and static interface methods |
| 53.0 | Java SE 9 | Modules: ACC_MODULE, the Module and Package constants, the Module, ModulePackages and ModuleMainClass attributes |
| 55.0 | Java SE 11 | Dynamic constants; NestHost and NestMembers |
| 56.0 | Java SE 12 | Minor version 65535 marks preview features |
| 60.0 | Java SE 16 | Record attribute |
| 61.0 | Java SE 17 | PermittedSubclasses (sealed classes); ACC_STRICT retired, as all floating point is strict |
The language features behind these are described on the Java versions page. Major versions 62 to 72 add nothing to the layout that this reference covers.
Telling it apart
The explorer’s parser for this format checks that the file starts with
CA FE BA BE and that the major version, the two bytes at offset 6, is 45
or more. It then reads the whole file strictly: every constant pool entry must have a
known tag, every attribute it knows must fill exactly the length it gives, every
method’s bytecode must be made of real instructions, and nothing may follow the last
attribute. A file that fails is turned down, and the explorer tries the
Oak class file parser.
On an Oak file, this parser reads the 4-byte Code header as if it were
8 bytes long. The code_length it finds is the start of the bytecode, which
runs past the end of the attribute, so the file is turned down. A version 45.0 to 45.2
file laid out as the specification says is read here.
Variants
- Java class file – versions 45.3 to 72, JDK 1.0.2 to Java SE 28, as the JVM Specification describes
- Oak class file (45.0 to 45.2) – pre-release classes whose Code attribute has a one-byte max_stack and max_locals
Links
Software
- OpenJDK – the reference Java platform, with the
javaccompiler, thejavaruntime and thejavapdisassembler (Windows, macOS, Linux) - IntelliJ IDEA – JetBrains’ development environment, which decompiles class files (Windows, macOS, Linux)
- Eclipse – free development environment for Java (Windows, macOS, Linux)
- JD-GUI – viewer that shows class files as Java source (Windows, macOS, Linux)
- CFR decompiler – command-line Java decompiler (any system with Java)
- Ghidra – the NSA’s free reverse engineering suite, which disassembles JVM bytecode (Windows, macOS, Linux)
- Jasmin – an assembler that turns a text form of JVM bytecode into class files (any system with Java)
Developers
- ASM – reads, writes and transforms class files (Java)
- Apache Commons BCEL – the Byte Code Engineering Library (Java)
- Javassist – edits class files at the source level or the bytecode level (Java)
- Class-File API – the JDK’s own library for reading and writing class files,
java.lang.classfile, final in JDK 24 (Java)
Reference
- The class File Format – chapter 4 of the Java Virtual Machine Specification (Java SE 25)
- The Java Virtual Machine Instruction Set – chapter 6, every instruction in detail
- Opcode Mnemonics by Opcode – chapter 7, the opcode table
- The javap command – the manual for the JDK’s class file disassembler
- classFileParser.cpp – HotSpot’s class file reader, with every version check