The Wayback Machine - https://web.archive.org/web/20240621053640/https://www.geeksforgeeks.org/java-class-file/
Open In App

Java Class File

Last Updated : 09 Jun, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

A Java class file is a file containing Java bytecode and having .class extension that can be executed by JVM. A Java class file is created by a Java compiler from .java files as a result of successful compilation. As we know that a single Java programming language source file (or we can say .java file) may contain one class or more than one class. So if a .java file has more than one class then each class will compile into a separate class files.
For Example: Save this below code as Test.java on your system.





For Compiling:

javac Test.java

After compilation there will be 3 class files in corresponding folder named as:

  • Sample.class
  • Student.class
  • Test.class

A single class file structure contains attributes that describe a class file.
Representation of Class File Structure

ClassFile 
{
     magic_number;
     minor_version;
     major_version;
     constant_pool_count;
     constant_pool[];
     access_flags;
     this_class;
     super_class;
     interfaces_count;
     interfaces[];
     fields_count;
     fields[];
     methods_count;
     methods[];
     attributes_count;
     attributes[];
}

Elements of class file are as follows:

  1. magic_number: The first 4 bytes of class file are termed as magic_number. This is a predefined value which the JVM use to identify whether the .class file is generated by valid compiler or not. The predefined value will be in hexadecimal form i.e. 0xCAFEBABE.
    Now let’s see what happen when JVM will not find valid magic number. Suppose we have a .java file named as Sample.java as follows and follow step by step process on your system.
  2. // class Declaration
    class Sample
    {
       public static void main(String[] args)
       {
           System.out.println("Magic Number");           
       }
    }
    

    Step 1: Compile using javac Sample.java
    Step 2: Now open the Sample.class file. It will looks like following.

    Sample.class File

    Step 3: Now erase at least single symbol from this Sample.class file from starting of file and save it.
    Step 4: Now try to run this using java Sample command and see the magic i.e. you will get run time exception (See the highlighted text in below image):
    Runtime Exception Due to Invalid magic number

    Note: This can vary depending on how much you remove the .class file data.

  3. minor_version & major_version: These both together represents .class file version. JVM will use these versions to identify which version of the compiler generates the current .class file. We denotes the version of class file as M.m where M stands for major_version and m stands for minor_version
  4. Note: Lower version compiler generated .class file can be executed by high version JVM but higher version compiler generated .class file cannot be executed by lower version JVM. If we will try to execute we will get run time exception.

    This demonstration is for Windows OS as follows:

    Step 1: Open a command prompt window and try to check java compiler version and JVM version using following commands respectively (Highlighted text in image are the commands)
    Output for 1.8 version will be:
    Java Compiler Version
    JVM Version

    Step 2: Now check with another version which may be higher or lower than already installed.thisDownload link.
    And install this to your PC or laptops and note the installation address.
    Step 3: Open a second command prompt window and set the path of bin folder of installed jdk installed during 2nd step. And check for Java compiler version ad JVM version.
    Checking Version of JDK 1.6
    Step 4: Now on 1st command prompt compile the any valid .java file. For example: See above Sample.java file. Compile it as:
    Compiling with Compiler Version 1.8
    Step 5: Now on 2nd command prompt window try to run the above compiled code class file and see what happen. There is a run time exception which I have highlighted in below image.

    Runtime Exception Due to Invalid major and minor Version of class file

    Note: Internally jdk 1.5 version means 49.0 and 1.6 means 50.0 and 1.7 means 51.0 etc. class file version where the digits before the decimal point represent the major_version and digits after decimal point represents the minor_version.

    1. constant_pool_count: It represents the number of the constants present in the constant pool (When a Java file is compiled, all references to variables and methods are stored in the class’s constant pool as a symbolic reference).
    2. constant_pool[]: It represents the information about constants present in constant pool file.
    3. access_flags: It provide the information about the modifiers which are declared to the class file.
    4. this_class: It represents fully qualified name of the class file.
    5. super_class: It represents fully qualified name of the immediate super class of current class. Consider above Sample.java file. When we will compile it, then we can say this_class will be Sample class and super_class will be Object class.
    6. interface_count: It returns the number of interfaces implemented by current class file.
    7. interface[]: It returns interfaces information implemented by current class file.
    8. fields_count: It represents the number of fields (static variable) present in current class file.
    9. fields[]: It represent fields (static variable) information present in current class file.
    10. method_count: It represents number of methods present in current class file.
    11. method[]: It returns information about all methods present in current class file.
    12. attributes_count: It returns the number of attributes (instance variables) present in current class file.
    13. attributes[]: It provides information about all attributes present in current class file.


Similar Reads

Java Program to Extract Content from a Java's .class File
In this article, we are going to extract the contents of the Java class file using the Apache Tika library. Apache Tika is used for document type detection and content extraction from various file formats. It uses various document parsers and document type detection techniques to detect and extract data. It provides a single generic API for parsing
2 min read
java.nio.file.attribute.FileTime Class in Java
The java.nio.file.attribute.FileTime class is used to get a value representing this file's timestamp i.e, the time when this file was last modified, accessed, or created. Class Declaration: public final class FileTime extends Object implements Comparable<FileTime>Methods: MethodDescriptioncompareTo(FileTime other)This method compares the valu
2 min read
java.nio.file.SimpleFileVisitor Class in Java
The java.nio.file.SimpleFileVisitor Class is used to visit all the files in a directory and to re-throw I/O exceptions whenever an I/O error occurs. Class declaration : public class SimpleFileVisitor<T> extends Object implements FileVisitor<T>Constructor: protected SimpleFileVIsitor(): Creates a new object of SimpleFileVisitor class.Met
3 min read
java.nio.file.Paths Class in Java
java.nio.file.Paths class contains static methods for converting path string or URI into Path. Class declaration : public final class Paths extends ObjectMethods: MethodDescriptionget(String first, String... more) This method converts a path string, or a sequence of strings that when joined form a path string, to a Path. get(URI uri) This method co
2 min read
java.nio.file.spi.FileTypeDetector Class in Java
java.nio.file.spi.FileTypeDetector Class extends java.lang.Object Class. FileTypeDetector Class contain method to know about the content type of given file. Class declaration: public abstract class FileTypeDetector extends ObjectConstructor: ConstructorDescriptionprotected FileTypeDetector()It is used the creation of a new object of FileTypeDetecto
2 min read
java.nio.file.LinkPermission Class in Java
The java.nio.file.LinkPermission class handles permissions for link creation operations. The permission allowed by these class are as follows: Permission name What permission allows Risk of granting this permission hard This permission allows adding an existing file to a directory. This operation is also known as creating a hard link. The attacker
3 min read
java.nio.file.FileStore Class in Java
Java.nio.file is a package in java that consists of the FileStore class. FileStore class is a class that provides methods for the purpose of performing some operations on file store. FileStore is a class that extends Object from java.lang package. And few methods the FileStore class can inherit from java.lang.Object package is clone(), equals(), fi
4 min read
java.nio.file.FileSystem class in java
java.nio.file.FileSystem class provides an interface to a file system. The file system acts as a factory for creating different objects like Path, PathMatcher, UserPrincipalLookupService, and WatchService. This object help to access the files and other objects in the file system. Syntax: Class declaration public abstract class FileSystem extends Ob
4 min read
java.nio.file.FileSystems Class in Java
java.nio.file.FileSystems class acts as a factory for creating new file systems. This class provides methods for creating file systems. This file system act as factories for creating different objects like Path, PathMatcher, UserPrincipalLookupService, and WatchService. This object helps to access the files and other objects in the file system. Syn
3 min read
java.nio.file.attribute.AclEntry Class in Java
ACL entries are Examined by this class is validating on the ACL model declared in RFC 3530: Network File System i.e.(NFS) version of 4th Protocol and having four components within it as follows characteristics as follows: This type of component are determining if the entry grants or denies its accessingThe principal component is generally known as
3 min read
Article Tags :
Practice Tags :