Introduction
A .class file holds one compiled Java class or interface: its methods as
bytecode for the Java Virtual Machine, plus the names, types and constants they use.
The Java compiler writes one class file per class, nested and anonymous classes
included, and the JVM loads them as the program needs them. Kotlin, Scala, Groovy,
Clojure and other JVM languages compile to the same format. The format belongs to the
Java platform, whose reference implementation is developed as
OpenJDK, and it is described in full in chapter 4
of the Java Virtual Machine Specification.
Class files are usually shipped packed inside a
.jar archive. Their source is usually a
.java file.
Every class file starts with CA FE BA BE and a version number. Since
1996 the format has grown by adding: new kinds of constant, new attributes, new flags
and one new instruction, which older readers can skip or never meet. One flag,
ACC_STRICT, was retired at version 61. The drafts for Java SE 28’s
preview features give one existing class flag bit a new meaning, add a field flag, an
attribute and a frame type, and change how six instructions behave, but only in class
files of exactly version 72.65535. Versions that only add share a
format; a version that changes an existing structure is a separate format. By that
rule there are two: the Java class file of JDK 1.0.2 and later, and the Oak class file
of the pre-release versions 45.0 to 45.2, whose Code attribute has a
shorter header.
A .class file dropped on any page of this site opens in the explorer,
which shows its structure, a disassembly in the style of javap -c -v and
the constant pool.
Variations
| Format | Variation | About | Open |
|---|---|---|---|
.CLASS | Java class file | The JVM Specification’s class file, from JDK 1.0.2 (45.3) to Java SE 28 (72). | Open |
.CLASS | Oak class file (45.0 to 45.2) | Pre-release Java classes whose Code attribute has a one-byte max_stack and max_locals. | Open |
Java class file
The class file as the Java Virtual Machine Specification describes it, written by
every Java compiler from JDK 1.0.2 in 1996 to today. It has grown by adding: the
constant pool gained six new kinds of entry, the list of attributes grew from six to
thirty, and one instruction, invokedynamic, was added. Each
addition is tied to a version, and a file uses only what its version allows.
The file starts with CA FE BA BE, and the version at offsets 4 to 7
is 45.3 or later, up to 72.0 (Java SE 28), or 72.65535 for a class that uses Java SE
28’s preview features. Files of 45.0 to 45.2 laid out as the specification says are
this format too. In every method’s Code attribute,
max_stack and max_locals are two bytes each and
code_length is four.
Oak class file (45.0 to 45.2)
The class files of Java before JDK 1.0.2, when the language was still called Oak.
They are laid out like a Java class file except in one place: in the
Code attribute, max_stack and max_locals are
one byte each and code_length is two bytes, so the attribute’s header
is 4 bytes instead of 8. No specification of this layout survives. It is known from
the reader in Java virtual machines up to JDK 13; OpenJDK removed it in JDK 14.
The version is 45.0, 45.1 or 45.2. Because the specification lets those versions
use the ordinary layout too, the version alone does not decide: the explorer reads a
file as a Java class file first, and only when its Code attributes do
not add up does it try the Oak layout. It uses only the constant pool tags 1 to 12
and the attributes JDK 1.0.2 knew.
Versions
Every class file version, with the Java release that introduced it. The version is
two big-endian 16-bit numbers at offsets 4 and 6: minor_version, then
major_version. From Java SE 9 the major version is the release number
plus 44. From Java SE 12 the minor version is 0, or 65535 for a class that uses the
preview features of exactly that release.
| Major | Minor | Release | Shipped | Format |
|---|---|---|---|---|
| 45 | 0 to 2 | Pre-release Java (Oak) | 1995 to 1996 | Oak class file or Java class file |
| 45 | 3 | JDK 1.0.2 and JDK 1.1 | 1996; February 1997 | Java class file |
| 46 | 0 | J2SE 1.2 | December 1998 | Java class file |
| 47 | 0 | J2SE 1.3 | May 2000 | Java class file |
| 48 | 0 | J2SE 1.4 | February 2002 | Java class file |
| 49 | 0 | J2SE 5.0 | September 2004 | Java class file |
| 50 | 0 | Java SE 6 | December 2006 | Java class file |
| 51 | 0 | Java SE 7 | July 2011 | Java class file |
| 52 | 0 | Java SE 8 | March 2014 | Java class file |
| 53 | 0 | Java SE 9 | September 2017 | Java class file |
| 54 | 0 | Java SE 10 | March 2018 | Java class file |
| 55 | 0 | Java SE 11 | September 2018 | Java class file |
| 56 | 0 or 65535 | Java SE 12 | March 2019 | Java class file |
| 57 | 0 or 65535 | Java SE 13 | September 2019 | Java class file |
| 58 | 0 or 65535 | Java SE 14 | March 2020 | Java class file |
| 59 | 0 or 65535 | Java SE 15 | September 2020 | Java class file |
| 60 | 0 or 65535 | Java SE 16 | March 2021 | Java class file |
| 61 | 0 or 65535 | Java SE 17 | September 2021 | Java class file |
| 62 | 0 or 65535 | Java SE 18 | March 2022 | Java class file |
| 63 | 0 or 65535 | Java SE 19 | September 2022 | Java class file |
| 64 | 0 or 65535 | Java SE 20 | March 2023 | Java class file |
| 65 | 0 or 65535 | Java SE 21 | September 2023 | Java class file |
| 66 | 0 or 65535 | Java SE 22 | March 2024 | Java class file |
| 67 | 0 or 65535 | Java SE 23 | September 2024 | Java class file |
| 68 | 0 or 65535 | Java SE 24 | March 2025 | Java class file |
| 69 | 0 or 65535 | Java SE 25 | September 2025 | Java class file |
| 70 | 0 or 65535 | Java SE 26 | March 2026 | Java class file |
| 71 | 0 or 65535 | Java SE 27 | September 2026 | Java class file |
| 72 | 0 or 65535 | Java SE 28 | in development, planned for March 2027 | Java class file |
A Java virtual machine runs every version from 45.0 up to its own. JDK 1.1 accepted 45.0 to 45.65535. Each release’s language changes are listed on the Java versions page.
CAFEBABE and Mach-O
Mach-O universal binaries, the “fat” files of macOS and NeXTSTEP that hold code for
several processors, start with the same four bytes, CA FE BA BE. In a
Mach-O universal binary the next four bytes are a
big-endian count of the architectures inside, usually 2 or 3. In a class file they
are the minor and major version.
So the 16-bit big-endian number at offset 6 tells them apart: 45 or more is a class
file’s major version; a universal binary’s architecture count is far smaller. The
explorer checks exactly this before it reads a file as a class file. A file that
starts like a class file but cannot be read is still shown as one if its name ends in
.class.
00000000 CA FE BA BE 00 03 00 2D 00 0F 07 00 0D 07 00 0E
00 2D), version 45.300000000 CA FE BA BE 00 00 00 03 01 00 00 07 00 00 00 03
/bin/ls, a Mach-O universal binary: the u16 at offset 6 is 3, three architectures (the first is x86-64, CPU type 01 00 00 07)Not described
Unknown
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 of the specification, every instruction
- The javap command – the manual for the JDK’s class file disassembler
- ClassFileFormatVersion – the JDK’s own list of class file versions, release by release
- 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 - JDK-8232890 – the OpenJDK bug that removed the Oak layout in JDK 14