Quick revision
The short answer
A class file starts with the magic number 0xCAFEBABE and a minor and major version, followed by the constant pool, the access flags, this class and its superclass, the interfaces, the fields, the methods and attributes such as Code, which holds each method's bytecode, a stack-based instruction set that javap -c -v shows. UnsupportedClassVersionError: class file version 65.0 means the class was compiled for Java 21 (61 is Java 17 and 69 is Java 25) and the JVM running it is older; we fix it by compiling with --release for the target version or by upgrading the runtime.
What to remember
In this answer, we walked through a class file: the magic number 0xCAFEBABE, the minor and major version, the constant pool, access flags, this and super class, interfaces, fields, methods and attributes such as Code. We read stack-based bytecode with javap -c -v, decoded major versions as the release plus 44 (61 for Java 17, 65 for 21, 69 for 25), understood that UnsupportedClassVersionError means a class was compiled for a newer Java than the runtime, and fixed it by upgrading the runtime or compiling with --release, which also limits the API.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A class file starts with the magic number 0xCAFEBABE and a minor and major version, followed by the constant pool, the access flags, this class and its superclass, the interfaces, the fields, the methods and attributes such as Code, which holds each method's bytecode, a stack-based instruction set that javap -c -v shows. UnsupportedClassVersionError: class file version 65.0 means the class was compiled for Java 21 (61 is Java 17 and 69 is Java 25) and the JVM running it is older; we fix it by compiling with --release for the target version or by upgrading the runtime.
Why this question is asked
Most developers never look inside a class file, until a deployment fails with UnsupportedClassVersionError and the number in the message means nothing to them. Knowing the class file layout also explains how frameworks inspect and generate code, why constants are inlined across classes, and what tools like javap show. Interviewers ask this to check that understanding and the practical fix. In this answer, we will walk through the structure of a class file, read a small method's bytecode with javap, decode the version numbers, and fix the error properly with --release.
1The Structure of a Class File
A class file is a binary file with a fixed order of sections, defined in the JVM specification. It always begins with four bytes, 0xCAFEBABE, the magic number that marks it as a class file, followed by two bytes of minor version and two bytes of major version. The major version says which Java release the file was compiled for, and a JVM refuses files newer than it understands.
Next comes the constant pool, a table of every constant the class refers to: string literals, numbers, and symbolic references to classes, fields and methods by name and type descriptor. Most other parts of the file point into this table by index. Then the access flags (public, final, interface, abstract and so on), this_class and super_class, which are indexes of the class's own name and its parent's name in the constant pool, the list of implemented interfaces, the fields, the methods, and finally attributes, extra named sections. The most important attribute is Code, attached to each method, which holds its bytecode, the maximum stack depth and the number of local variables. Others include SourceFile, LineNumberTable for stack traces, and BootstrapMethods for invokedynamic.
| Section | Contents | Example |
|---|---|---|
| magic | 0xCAFEBABE | CA FE BA BE |
| minor_version, major_version | Class file version | 0, 65 for Java 21 |
| constant_pool | Literals and symbolic references | #7 = Methodref Point.add |
| access_flags, this_class, super_class | Modifiers and names of class and parent | public, Point, java/lang/Object |
| interfaces, fields | Implemented interfaces, declared fields | int x, int y |
| methods | Each method with its attributes | add(II)I with a Code attribute |
| attributes | SourceFile, BootstrapMethods, ... | SourceFile: Point.java |
2Reading Bytecode With javap
The JDK's javap tool disassembles class files. With -c it prints the bytecode of each method, with -v it adds the version, flags and the constant pool, and with -p it includes private members. Let us compile a tiny class and look.
Notice that bytecode is stack-based. There are no registers; instructions push values onto an operand stack and pop them off. iload_0 pushes the first local variable, iadd pops two ints and pushes their sum. The Code attribute records that this method needs at most 2 stack slots and 2 local variable slots, which the verifier checks. The descriptor (II)I is the compact type signature stored in the constant pool.
3Decoding the Version Number
The major version is the Java release plus 44. Java 8 is 52, Java 11 is 55, Java 17 is 61, Java 21 is 65 and Java 25 is 69. The minor version is 0 for normal classes; a class compiled with preview features enabled has minor version 65535 and only runs on exactly that release with --enable-preview.
| Java release | Major version | Note |
|---|---|---|
| 8 | 52 | Still common in old systems |
| 11 | 55 | LTS |
| 17 | 61 | LTS, Spring Boot 3 minimum |
| 21 | 65 | LTS, virtual threads final |
| 25 | 69 | LTS, current |
So the message tells us both sides: the class needs Java 21 (65.0), and the runtime is Java 17 (it understands up to 61.0). The JVM refuses rather than guessing, because a newer class file may use instructions, attributes or constant pool entries the older JVM does not know.
4Fixing It Properly With --release
There are two directions. If the runtime is supposed to be Java 21, upgrade it, usually by changing the base image, and the error disappears. If the runtime must stay on 17, compile for 17. The correct option is --release 17, not -source and -target. The reason is that --release also restricts the compiler to the Java 17 API: with only -target, a Java 21 compiler would happily compile a call to a method added in Java 21, and the program would fail later with NoSuchMethodError on the Java 17 runtime.
The same error also appears when only one dependency was built for a newer Java than the runtime. In that case the class named in the message belongs to the library, and the fix is to use a library version that supports our runtime, or to upgrade the runtime. Checking the major version with javap -v or the first eight bytes of the class file tells us quickly which JAR is responsible.
5Follow-up questions
Why is the magic number 0xCAFEBABE?
It was chosen by the original Java team as a memorable hexadecimal value; it has no technical meaning beyond identifying the file type. Every valid class file starts with it, and the JVM rejects a file that does not, with a ClassFormatError about an incompatible magic value.
What is the difference between UnsupportedClassVersionError and NoSuchMethodError after an upgrade?
UnsupportedClassVersionError is thrown while loading, when the class file version is newer than the JVM supports. NoSuchMethodError is thrown later, when code calls a method that does not exist in the runtime's version of a class, typically because the code was compiled against a newer API with -target instead of --release, or against a different library version.
Can a newer JVM run old class files?
Yes. JVMs accept all older class file versions, so a class compiled for Java 8 runs on Java 25. Problems after upgrades come from removed or encapsulated APIs, or from tools and libraries that read bytecode and do not support newer versions, not from the old class files themselves.
6Conclusion
In this answer, we walked through a class file: the magic number 0xCAFEBABE, the minor and major version, the constant pool, access flags, this and super class, interfaces, fields, methods and attributes such as Code. We read stack-based bytecode with javap -c -v, decoded major versions as the release plus 44 (61 for Java 17, 65 for 21, 69 for 25), understood that UnsupportedClassVersionError means a class was compiled for a newer Java than the runtime, and fixed it by upgrading the runtime or compiling with --release, which also limits the API.
Open with
A class file is a binary format with a fixed layout. It starts with the magic number 0xCAFEBABE, then a minor and a major version. Then comes the constant pool, a table of literals and symbolic references to classes, fields and methods, which the rest of the file refers to by index.
Then, if they want more
After that the access flags, this class and the superclass, the interfaces, the fields, the methods, and attributes. Each method has a Code attribute with its bytecode, the maximum stack depth and the number of locals, and there are attributes like SourceFile, LineNumberTable and BootstrapMethods.
The bytecode is a stack-based instruction set: for a + b we get iload_0, iload_1, iadd, ireturn. javap -c shows the bytecode, -v adds the version and constant pool, and -p includes private members. The major version is the Java release plus 44, so 61 is Java 17, 65 is Java 21 and 69 is Java 25.
UnsupportedClassVersionError with class file version 65.0 means the class was compiled for Java 21 and the JVM running it is older, and the message also says the highest version it accepts, for example 61 for Java 17.
The fix is either to upgrade the runtime, usually the base image, or to compile for the target with --release 17, through maven.compiler.release or a Gradle toolchain. I prefer --release over -target because it also restricts the API, so we do not get NoSuchMethodError later.
If the class is from a dependency, I need a library version built for our runtime.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 17, Java 21, Java 25, Java 8
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What is in a Java class file, and what does UnsupportedClassVersionError: class file...": https://scrivio-interview-library.netlify.app/java/jvm-architecture/50bf975d7d/