Java class file: Difference between revisions

Content deleted Content added
Adding local short description: "Executable Java file format", overriding Wikidata description "file format"
 
(17 intermediate revisions by 16 users not shown)
Line 23:
}}
 
A '''Java class file''' is a [[Computer file|file]] (with the {{mono|.class}} [[filename extension]]) containing [[Java bytecode]] that can be executed on the [[Java virtual machine|Java Virtual Machine (JVM)]]. A Java class file is usually produced by a [[Java compiler]] from [[Java (programming language)|Java programming language]] [[source file]]s ({{mono|.java}} files) containing Java [[Class (programming)|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. Thus, it is called a {{mono|.class}} file because it contains the bytecode for a single class.
 
JVMs are available for many [[platform (computing)|platform]]s, and a class file compiled on one platform will execute on a JVM of another platform. This makes Java applications [[cross-platform|platform-independent]].
Line 34:
===Sections===
There are 10 basic sections to the Java class file structure:
* '''[[Magic number (programming)|Magic Number]]''': <code>0xCAFEBABE</code>
* '''Version of Class File Format''': the minor and major versions of the class file
* '''Constant Pool''': Pool of constants for the class
Line 42:
* '''[[Interface (object-oriented programming)|Interfaces]]''': Any interfaces in the class
* '''Fields''': Any fields in the class
* '''[[Method (computingcomputer programming)|Method]]s''': Any methods in the class
* '''Attributes''': Any attributes of the class (for example the name of the sourcefile, etc.)
 
Line 66:
{| class="wikitable"
|-
! byteByte offset
! sizeSize
! typeType or value
! Description
! description
|-
| 0
Line 95:
| rowspan="2" | 2 bytes
| rowspan="2" | u2
| rowspan="2" | major version number of the class file format being used.<ref>{{Cite web|url=https://docs.oracle.com/javase/specs/jvms/se17se23/html/jvms-4.html#jvms-4.1-200-B.2|title = ChapterTable 4. The1-A. class Filefile format major Formatversions}}</ref><br />
Java SE 25 = 69 (0x45 hex),<br />
Java SE 24 = 68 (0x44 hex),<br />
Java SE 23 = 67 (0x43 hex),<br />
Java SE 22 = 66 (0x42 hex),<br />
Java SE 21 = 65 (0x41 hex),<br />
Java SE 20 = 64 (0x40 hex),<br />
Java SE 19 = 63 (0x3F hex),<br />
Java SE 18 = 62 (0x3E hex),<br />
Line 226 ⟶ 232:
|}
 
===Representation inof a C-likeclass programming languagefile===
The following is a representation of a {{mono|.class}} file as if it were a C-style struct.
Since [[C (programming language)|C]] doesn't support multiple variable length arrays within a struct, the code below won't compile and only serves as a demonstration.
<syntaxhighlight lang="ccpp">
struct Class_File_FormatClassFileFormat {
u4 magic_numbermagicNumber;
 
u2 minor_versionminorVersion;
u2 major_versionmajorVersion;
 
u2 constant_pool_countconstantPoolCount;
ConstantPoolInfo[constantPoolCount - 1] constantPool;
cp_info constant_pool[constant_pool_count - 1];
 
u2 access_flagsaccessFlags;
 
u2 this_classthisClass;
u2 super_classsuperClass;
 
u2 interfaces_countinterfacesCount;
u2[interfacesCount] interfaces;
u2 interfaces[interfaces_count];
 
u2 fields_countfieldsCount;
FieldInfo[fieldsCount] fields;
field_info fields[fields_count];
 
u2 methods_countmethodsCount;
MethodInfo[methodsCount] methods;
method_info methods[methods_count];
 
u2 attributes_countattributesCount;
AttributeInfo[attributesCount] attributes;
attribute_info attributes[attributes_count];
}
</syntaxhighlight>