Why are you trying to load a .class file
A
Java class file is a
file (with the .class
filename extension) containing
Java bytecode that can be executed on the
Java Virtual Machine (JVM). A Java class file is usually produced by a
Java compiler from
Java programming language source files (.java files) containing Java
classes (alternatively, other
JVM languages can also be used to create class files). If a source file has more than one class, each class is compiled into a separate class file.
JVMs are available for many
platforms, and a class file compiled on one platform will execute on a JVM of another platform. This makes Java applications
platform-independent.
History
On 11 December 2006, the class file format was modified under
Java Specification Request (JSR) 202.
[1]
File layout and structure
Sections
There are 10 basic sections to the Java Class File structure:
- Magic Number: 0xCAFEBABE
- Version of Class File Format: the minor and major versions of the class file
- Constant Pool: Pool of constants for the class
- Access Flags: for example whether the class is abstract, static, etc.
- This Class: The name of the current class
- Super Class: The name of the super class
- Interfaces: Any interfaces in the class
- Fields: Any fields in the class
- Methods: Any methods in the class
- Attributes: Any attributes of the class (for example the name of the sourcefile, etc.)
Magic Number
Class files are identified by the following 4
byte header (in
hexadecimal): CA FE BA BE (the first 4 entries in the table below). The history of this
magic number was explained by
James Gosling referring to a restaurant in
Palo Alto:
[2]
"We used to go to lunch at a place called St Michael's Alley. According to local legend, in the deep dark past, the
Grateful Dead used to perform there before they made it big. It was a pretty funky place that was definitely a Grateful Dead Kinda Place. When
Jerry died, they even put up a little Buddhist-esque shrine. When we used to go there, we referred to the place as Cafe Dead. Somewhere along the line it was noticed that this was a HEX number. I was re-vamping some file format code and needed a couple of
magic numbers: one for the persistent object file, and one for classes. I used CAFEDEAD for the object file format, and in
grepping for 4 character hex words that fit after "CAFE" (it seemed to be a good theme) I hit on BABE and decided to use it. At that time, it didn't seem terribly important or destined to go anywhere but the trash-can of history. So CAFEBABE became the class file format, and CAFEDEAD was the persistent object format. But the persistent object facility went away, and along with it went the use of CAFEDEAD - it was eventually replaced by RMI.
General layout
Because the class file contains variable-sized items and does not also contain embedded file offsets (or pointers), it is typically parsed sequentially, from the first byte toward the end. At the lowest level the file format is described in terms of a few fundamental data types:
- u1: an unsigned 8-bit integer
- u2: an unsigned 16-bit integer in big-endian byte order
- u4: an unsigned 32-bit integer in big-endian byte order