What is a class file?
A .class file contains Java bytecode: the compact, platform-independent code that the Java Virtual Machine (JVM) actually executes. When you compile a .java source file with javac, the output is a .class file — one per class or interface. The format is the reason Java is "write once, run anywhere": the same bytecode runs unchanged on any JVM, on any operating system.
Class files are binary. Open one in a text editor and you get mojibake; the bytes are instructions and tables, not characters. To read a class file you use a decompiler, which reconstructs readable Java from the bytecode — you can decompile class files online here, entirely in your browser, or keep reading for what is actually inside the format.
Inside a .class file: structure
The class file format is a strictly ordered binary layout, defined by the JVM specification. A reader walks it top to bottom:
- Magic number — the four bytes
0x CA FE BA BE("cafe babe"). Every valid class file starts with it; it is how tools recognize the format instantly. - Version numbers — a minor and a major version, two bytes each. The major version maps to the Java language level: 50 = Java 6, 52 = Java 8, 55 = Java 11, 61 = Java 17, 65 = Java 21. A JVM refuses to load bytecode newer than itself — the source of the classic "unsupported class file version" error.
- Constant pool — the class’s table of strings: class and interface names, field and method names, type descriptors, string literals, numeric constants. Everything the class refers to by name lives here once, indexed by number.
- Access flags & declarations — whether the class is public, final, abstract, an interface or enum; then this class, its superclass, and its interfaces (as constant-pool references).
- Fields and methods — each with flags (private, static, final…), a name, a type descriptor, and optional attributes. Method bodies are stored as a
Codeattribute containing the bytecode instructions. - Attributes — everything optional-but-important:
LineNumberTableandLocalVariableTable(the debug information that lets decompilers recover variable names), annotations, and generic signature data.
Bytecode: instructions for a stack machine
Method bodies are sequences of JVM instructions. Unlike a real CPU, the JVM is a stack machine: instructions push and pop values on an operand stack instead of writing registers. Adding two numbers is iload_0 (push local 0), iload_1 (push local 1), iadd (pop both, push the sum), istore_2 (store into local 2). There are around two hundred opcodes covering loads, arithmetic, field access, method calls, object creation, branches, and exception handling.
Two properties of this design matter in practice. First, it is deliberately verifiable: the JVM checks every method’s bytecode before running it (types line up, stack use is consistent), which is a large part of Java’s memory-safety story. Second, it retains a lot of structure — names, types, and control flow are preserved — which is exactly what makes decompilation to readable source possible at all.
.java, .class, and .jar: the triangle
The three Java file types form a one-way pipeline with a decompiler shortcut back:
A `.java` file is human-written source — plain text, one public class per file by convention. A `.class` file is what javac turns it into: binary bytecode for the JVM. A `.jar` file is many class files (plus resources and a manifest) zipped into one deliverable — what is a JAR file covers that container in depth.
The forward direction (source → class → jar) is compilation and packaging. The backward direction is decompilation: a tool that reads bytecode and reconstructs equivalent Java source. It cannot recover what compilation discarded — comments, formatting, and typically local variable names — but with debug information present (the default for most builds) it gets remarkably close. You can try it without installing anything: decompile a class file online or decompile a whole JAR online, in your browser, no upload.
How to open a .class file
"Opening" a class file means one of two very different things — reading it or running it. Reading is what decompilers and viewers do; running executes it on a JVM. The distinction matters for safety: reading never executes the code, running it does, with all your user permissions.
- Decompile it (read as Java): the practical choice. A decompiler like CFR reconstructs the source; ZipTool runs it in your browser, so the file is not uploaded anywhere.
- Disassemble it (read as bytecode): the JDK’s
javap -c Main.classprints the raw instructions — opcode by opcode, useful for compiler-level debugging, not for reading logic. - Run it:
java Main(the class must be on the classpath; for jars,java -jar app.jarreads the manifest’s Main-Class). Only run class files you trust. - Hex-inspect it: the first bytes should read
CA FE BA BE— a quick way to confirm a download is actually a class file before anything else.
Version table: which Java compiled this?
Bytes 4–7 of a class file give its minor and major version. The major version tells you exactly which language level the compiler targeted — handy when a jar refuses to run on an older runtime:
- Java 8 → major 52 · Java 11 → major 55 · Java 17 → major 61 · Java 21 → major 65
- Older but still common: Java 5 → 49, Java 6 → 50, Java 7 → 51
- A JVM loads bytecode with major version ≤ its own. Java 8 runs major-50 classes fine; the reverse fails with
UnsupportedClassVersionError.
Security: opening is not running
A class file is data until a JVM executes it. Decompiling, disassembling, or hex-dumping a class file parses bytes — nothing in it runs, so inspecting even a malicious class is safe in itself. That is why "read it before you trust it" is reasonable advice for jars from unknown sources.
The line is crossed when you *run* the code: java -jar on an untrusted jar executes it with your permissions, and the sandbox of the browser you inspected it in is irrelevant at that point. Inspect freely, run cautiously. More in security and privacy.
One privacy note that applies to the tooling itself: many online decompilers ask you to upload the class file to their server. If the code is sensitive — proprietary, under NDA, a client’s build — prefer an in-browser decompiler where the file never leaves your device (decompile class files online).
Frequently asked questions
What is a .class file?
A .class file contains compiled Java bytecode — the binary, platform-independent instructions the Java Virtual Machine executes. Compiling a .java source file with javac produces one .class file per class. Every class file starts with the magic bytes CA FE BA BE and encodes a version, a constant pool, and the class’s fields, methods, and bytecode.
How do I open a .class file?
To read it, decompile it: a decompiler (like CFR) reconstructs readable Java source from the bytecode — ZipTool does this in your browser with no upload. To see raw instructions, use the JDK’s javap -c. To run it, use java with the class on the classpath. Opening a class file for reading never executes it.
What is the difference between .java, .class, and .jar?
.java is human-readable source code. .class is the compiled bytecode the JVM executes, produced by javac from a .java file. .jar is a zip archive bundling many .class files (plus resources and a manifest) for distribution. Source compiles to classes, classes package into jars; a decompiler goes backwards from class to readable source.
What does CAFEBABE mean?
It is the class file magic number: every valid .class file starts with the four bytes 0x CA FE BA BE — a pun on "café babe". The bytes exist so tools can instantly recognize the format, which is also how a viewer can tell a real class file from a renamed or corrupted one before decompiling.
Can I get the original source back from a .class file?
Not the exact original: comments, formatting, and most local variable names are discarded during compilation and cannot be recovered. A decompiler reconstructs *equivalent* source — same classes, methods, and behavior — and when the class was compiled with debug information (most are), variable names and generics are recovered too. You can decompile class files in your browser at decompile class files online.
Is it safe to open a .class file?
Reading a class file — decompiling, disassembling, or viewing it — is safe: the bytecode is parsed, never executed. Running it (java) is the risky part, since that executes the code with your permissions. Inspect unfamiliar code first, run it only if you trust what you read.