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 later releases have added to it: new
kinds of constant, new attributes, new flags and one new instruction. The layout of
what was already there has not changed. Two things go the other way: the flag
ACC_STRICT was retired at version 61, and the
drafts for Java SE 28’s preview features change the meaning
of one existing class flag bit, and how six instructions behave, in version 72.65535
files only. 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.0, or 72.65535 for a Java SE 28 preview class4 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, unless the row says otherwise: 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 |
| 72.65535 | Java SE 28 (preview, draft) | Not final: ACC_IDENTITY, ACC_STRICT_INIT, LoadableDescriptors, early_larval_frame |
The language features behind these are described on the
Java versions page. Major versions 62 to 71
add no constant tags, attributes or flags: the specifications of Java SE 25 and 27
list nothing after PermittedSubclasses.
Version 72.65535: the Java SE 28 preview drafts
Java SE 28 is in development. Its class file format so far is the 45.3 to 71 format above, except in class files with major version 72 and minor version 65535, which depend on preview features. For those, two published drafts change the format. They are drafts and may change before release, and they apply only to exactly 72.65535: a bit or a frame type described below means nothing in any other version.
- JEP 401, value objects:
the bit 0x0020 on a class,
ACC_SUPERelsewhere, becomes ACC_IDENTITY, which marks an identity class; without it a class is a value class. Fields gain ACC_STRICT_INIT, 0x0800. A new attribute, LoadableDescriptors, may appear on a class. Four instructions behave differently on value objects: if_acmpeq and if_acmpne compare two value objects of the same class by their fields, monitorenter throwsIdentityExceptionand monitorexit throwsIllegalMonitorStateException. - JEP 539, strict field initialisation:
StackMapTablegains early_larval_frame, frame type 246. While a class is being initialised, getstatic throwsIllegalStateExceptionfor an ACC_STRICT_INIT static field that has not been set, putstatic throws it for a final one set after it has been read, and initialisation fails withExceptionInInitializerErrorif one is left unset. The draft says the exception class may change.
The explorer reads all of these in a 72.65535 file, and treats the flags, attribute and frame type as not defined in every other file. In a 72.65535 file it adds the drafts’ behaviour to these six instructions’ descriptions.
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 27)
- 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
- Value Objects (JEP 401, draft) – the JDK 28 draft changes for class file version 72.65535
- Strict field initialization (JEP 539, draft) – the JDK 28 draft that adds
early_larval_frame