A class file is a single structure, which the specification calls
ClassFile. It has no padding and no alignment, and every number in it is
big-endian. Types are written u1, u2 and u4 for
unsigned numbers of one, two and four bytes.
00000000 CA FE BA BE 00 00 00 3D 00 79 01 00 2B 63 6F 6D
Fields
Only the first four fields have fixed offsets. The constant pool’s entries vary in size, so the rest must be found by reading through it.
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 4 | u4 | magic | CA FE BA BE |
| 4 | 2 | u2 | minor_version | The minor version: 3 for JDK 1.0.2 and 1.1 compilers, 0 for later ones, 65535 for a class that uses preview features (version 56 and later) |
| 6 | 2 | u2 | major_version | The major version, 45 to 72 |
| 8 | 2 | u2 | constant_pool_count | One more than the number of constant pool indexes; entries are numbered 1 to count − 1 |
| 10 | varies | cp_info[] | constant_pool | The constant pool |
| … | 2 | u2 | access_flags | The class’s access and property flags |
| … | 2 | u2 | this_class | Index of a Class entry: this class |
| … | 2 | u2 | super_class | Index of a Class entry: the direct superclass; 0 only for java/lang/Object and module-info |
| … | 2 | u2 | interfaces_count | The number of direct superinterfaces |
| … | 2 × n | u2[] | interfaces | Indexes of Class entries, one per direct superinterface |
| … | 2 | u2 | fields_count | The number of fields declared by this class |
| … | varies | field_info[] | fields | See fields and methods |
| … | 2 | u2 | methods_count | The number of methods, constructors and initialisers |
| … | varies | method_info[] | methods | See fields and methods |
| … | 2 | u2 | attributes_count | The number of class attributes |
| … | varies | attribute_info[] | attributes | The class’s attributes |
Nothing may follow the last attribute. HotSpot turns such a file down with the message “Extra bytes at the end of class file”; the explorer shows the bytes as “Extra bytes” and marks them.
magic
The first four bytes are always CA FE BA BE. A Mach-O universal binary
starts with the same four bytes; the
family page explains how to tell them apart.
minor_version and major_version
Together they give the version as major.minor, such as 45.3 or 61.0. The major version says which features the file may use and which rules the JVM checks; the table of every version is on the family page. From Java SE 9, the major version is the Java release plus 44.
- For major versions 45 to 55 the minor version may be any value. Compilers of JDK 1.0.2 and 1.1 wrote 45.3; later compilers write 0.
- From major version 56 (Java SE 12), the minor version must be 0 or 65535. 65535 means the class uses preview features, and only the JVM of exactly that release with preview features turned on will load it.
- A JVM loads every major version from 45 up to its own, and turns down newer ones.
this_class
An index of a Class
entry naming this class or interface, in internal form, such as
java/util/EmptyStackException. A class file has to be found under the same
name: the class loader looks for java/util/EmptyStackException.class.
For a module declaration the name is module-info.
super_class
An index of a Class entry naming the direct superclass. It is 0 only in
java/lang/Object, which has none, and in a module-info file. An interface’s
super_class is always java/lang/Object. The superclass may not be final
nor an interface; the JVM checks this when the class is loaded.
interfaces
interfaces_count is followed by that many two-byte indexes of Class
entries. They are the interfaces in the class’s implements clause, or an
interface’s extends clause, in the order of the source. Interfaces
inherited from superclasses are not listed.
Limits
The sizes of the fields set limits on what a class can hold (JVM Specification §4.11):
- at most 65,535 constant pool slots, interfaces, fields and methods (the counts are two bytes; the constant pool has 65,534 usable slots, because its count is one more than the number of slots);
- a method’s bytecode is 1 to 65,535 bytes, since the branch and exception table offsets are two bytes;
max_stackandmax_localsare at most 65,535;- a method’s parameters take at most 255 local variable slots, counting
thisas one and eachlonganddoubleas two; - an array type has at most 255 dimensions;
- a Utf8 constant is at most 65,535 bytes, which limits the length of names and string literals.
Names
Names are stored in Utf8 entries, in these forms (JVM Specification §4.2):
- Class and interface names are in internal form: the fully
qualified name with
/instead of.between packages, as injava/lang/Thread. A nested class has its binary name, such asjava/util/Map$Entry. In a Class entry an array type is written as its descriptor instead, such as[Ljava/lang/String;. - Field, method, local variable and parameter names are unqualified
names. They may not contain
.,;,[or/. Method names also may not contain<or>, except the two special names<init>(a constructor) and<clinit>(the class or interface initialiser). - Module names keep their dots, such as
java.base. Package names in Package entries use/, such asjava/lang.
The source-level rules for names and packages are on the Java names and packages and modules pages.
A complete file
This is java/util/EmptyStackException.class from JDK 1.1.4, all 248 bytes
of it. Its source is a public class with one constructor that does nothing but call its
superclass’s.
00000000 CA FE BA BE 00 03 00 2D 00 0F 07 00 0D 07 00 0E .......-........
00000010 0A 00 01 00 04 0C 00 06 00 05 01 00 03 28 29 56 .............()V
00000020 01 00 06 3C 69 6E 69 74 3E 01 00 04 43 6F 64 65 ...<init>...Code
00000030 01 00 0D 43 6F 6E 73 74 61 6E 74 56 61 6C 75 65 ...ConstantValue
00000040 01 00 18 45 6D 70 74 79 53 74 61 63 6B 45 78 63 ...EmptyStackExc
00000050 65 70 74 69 6F 6E 2E 6A 61 76 61 01 00 0A 45 78 eption.java...Ex
00000060 63 65 70 74 69 6F 6E 73 01 00 0E 4C 6F 63 61 6C ceptions...Local
00000070 56 61 72 69 61 62 6C 65 73 01 00 0A 53 6F 75 72 Variables...Sour
00000080 63 65 46 69 6C 65 01 00 1A 6A 61 76 61 2F 6C 61 ceFile...java/la
00000090 6E 67 2F 52 75 6E 74 69 6D 65 45 78 63 65 70 74 ng/RuntimeExcept
000000A0 69 6F 6E 01 00 1D 6A 61 76 61 2F 75 74 69 6C 2F ion...java/util/
000000B0 45 6D 70 74 79 53 74 61 63 6B 45 78 63 65 70 74 EmptyStackExcept
000000C0 69 6F 6E 00 01 00 02 00 01 00 00 00 00 00 01 00 ion.............
000000D0 01 00 06 00 05 00 01 00 07 00 00 00 11 00 01 00 ................
000000E0 01 00 00 00 05 2A B7 00 03 B1 00 00 00 00 00 01 .....*..........
000000F0 00 0C 00 00 00 02 00 09 ........
| Offset | Bytes | Field | Value |
|---|---|---|---|
| 0x00 | CA FE BA BE | magic | |
| 0x04 | 00 03 | minor_version | 3 |
| 0x06 | 00 2D | major_version | 45: version 45.3 |
| 0x08 | 00 0F | constant_pool_count | 15: entries #1 to #14 |
| 0x0A | 07 00 0D | #1 Class | name #13, java/lang/RuntimeException |
| 0x0D | 07 00 0E | #2 Class | name #14, java/util/EmptyStackException |
| 0x10 | 0A 00 01 00 04 | #3 Methodref | class #1, name and type #4 |
| 0x15 | 0C 00 06 00 05 | #4 NameAndType | name #6, descriptor #5 |
| 0x1A | 01 00 03 … | #5 Utf8 | ()V |
| 0x20 | 01 00 06 … | #6 Utf8 | <init> |
| 0x29 | 01 00 04 … | #7 Utf8 | Code |
| 0x30 | 01 00 0D … | #8 Utf8 | ConstantValue (not used) |
| 0x40 | 01 00 18 … | #9 Utf8 | EmptyStackException.java |
| 0x5B | 01 00 0A … | #10 Utf8 | Exceptions (not used) |
| 0x68 | 01 00 0E … | #11 Utf8 | LocalVariables (not used) |
| 0x79 | 01 00 0A … | #12 Utf8 | SourceFile |
| 0x86 | 01 00 1A … | #13 Utf8 | java/lang/RuntimeException |
| 0xA3 | 01 00 1D … | #14 Utf8 | java/util/EmptyStackException |
| 0xC3 | 00 01 | access_flags | ACC_PUBLIC; this compiler did not set ACC_SUPER |
| 0xC5 | 00 02 | this_class | #2, java/util/EmptyStackException |
| 0xC7 | 00 01 | super_class | #1, java/lang/RuntimeException |
| 0xC9 | 00 00 | interfaces_count | 0 |
| 0xCB | 00 00 | fields_count | 0 |
| 0xCD | 00 01 | methods_count | 1 |
| 0xCF | 00 01 00 06 00 05 00 01 | method 0 | public, name #6 <init>, descriptor #5 ()V, 1 attribute |
| 0xD7 | 00 07 00 00 00 11 | its attribute | name #7 Code, 17 bytes long |
| 0xDD | 00 01 00 01 00 00 00 05 | Code header | max_stack 1, max_locals 1, code_length 5 |
| 0xE5 | 2A B7 00 03 B1 | code | aload_0; invokespecial #3; return |
| 0xEA | 00 00 00 00 | Code tail | no exception handlers, no attributes |
| 0xEE | 00 01 | attributes_count | 1 |
| 0xF0 | 00 0C 00 00 00 02 00 09 | attribute 0 | name #12 SourceFile, 2 bytes: #9 EmptyStackException.java |
The compiler of this time put a few Utf8 entries in the pool that nothing uses, such as
ConstantValue and LocalVariables. That is allowed: the pool
may hold anything, used or not.
javap -c -v shows the method like this, and the explorer shows the same
text:
public java.util.EmptyStackException();
descriptor: ()V
flags: (0x0001) ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #3 // Method java/lang/RuntimeException."<init>":()V
4: return