Last Updated : 21 May, 2025
JVM (Java Virtual Machine) runs Java applications as a run-time engine. JVM is the one that calls the main method present in a Java code. JVM is a part of JRE (Java Runtime Environment). Java applications are called WORA (Write Once Run Anywhere). This means a programmer can develop Java code on one system and expect it to run on any other Java-enabled system without any adjustments. This is all possible because of the JVM. When we compile a .java file, .class files (containing byte-code) with the same class names present in the .java file are generated by the Java compiler. This .class file goes through various steps when we run it. These steps together describe the whole JVM.
Architecture of JVMThe image below demonstrates the architecture and key components of JVM.
Core Components Of JVMNow, we are going to discuss each component of the JVM in detail.
1. Class Loader SubsystemIt is mainly responsible for three activities.
1. Loading: The Class loader reads the “.class” file, generate the corresponding binary data and save it in the method area. For each “.class” file, JVM stores the following information in the method area.
After loading the “.class” file, JVM creates an object of type Class to represent this file in the heap memory. Please note that this object is of type lass predefined in java.lang package. These Class object can be used by the programmer for getting class level information like the name of the class, parent name, methods and variable information etc. To get this object reference we can use getClass() method of Object class.
Example:
Java
// A Java program to demonstrate working
// of a Class type object created by JVM
// to represent .class file in memory
import java.lang.reflect.Field;
import java.lang.reflect.Method;
// Java code to demonstrate use
// of Class object created by JVM
public class Geeks
{
public static void main(String[] args)
{
Student s1 = new Student();
// Getting hold of Class
// object created by JVM.
Class c1 = s1.getClass();
// Printing type of object using c1.
System.out.println(c1.getName());
// getting all methods in an array
Method m[] = c1.getDeclaredMethods();
for (Method method : m)
System.out.println(method.getName());
// getting all fields in an array
Field f[] = c1.getDeclaredFields();
for (Field field : f)
System.out.println(field.getName());
}
}
// A sample class whose information
// is fetched above using its Class object.
class Student {
private String name;
private int roll_No;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getRoll_no() { return roll_No; }
public void setRoll_no(int roll_no)
{
this.roll_No = roll_no;
}
}
Student getName setName getRoll_no setRoll_no name roll_No
Note: For every loaded “.class” file, only one object of the class is created.
Student s2 = new Student();
// c2 will point to same object where
// c1 is pointing
Class c2 = s2.getClass();
System.out.println(c1==c2); // true
2. Linking: Performs verification, preparation, and (optionally) resolution.
3. Initialization: In this phase, all static variables are assigned with their values defined in the code and static block(if any). This is executed from top to bottom in a class and from parent to child in the class hierarchy. In general, there are three class loaders:
Example:
Java
// Java code to demonstrate Class Loader subsystem
public class Geeks
{
public static void main(String[] args)
{
// String class is loaded by bootstrap loader, and
// bootstrap loader is not Java object, hence null
System.out.println(String.class.getClassLoader());
// Test class is loaded by Application loader
System.out.println(Geeks.class.getClassLoader());
}
}
null jdk.internal.loader.ClassLoaders$AppClassLoader@8bcc55f
Note: JVM follows the Delegation-Hierarchy principle to load classes. System class loader delegate load request to extension class loader and extension class loader delegate request to the bootstrap class loader. If a class found in the boot-strap path, the class is loaded otherwise request again transfers to the extension class loader and then to the system class loader. At last, if the system class loader fails to load class, then we get run-time exception java.lang.ClassNotFoundException.
2. Class LoadersThere are three primary types of class loaders:
JAVA_HOME/lib
directory. It is implemented in native code and is not a Java object.JAVA_HOME/jre/lib/ext
directory or any directory specified by the java.ext.dirs
system property. It is implemented in Java.java.class.path
environment variable. It is also implemented in Java.Example:
3. JVM Memory Areaspublic class Test {
public static void main(String[] args) {
System.out.println(String.class.getClassLoader());
System.out.println(Test.class.getClassLoader());
}
}
Execution engine executes the “.class” (bytecode). It reads the byte-code line by line, uses data and information present in various memory area and executes instructions. It can be classified into three parts:
It is an interface that interacts with the Native Method Libraries and provides the native libraries(C, C++) required for the execution. It enables JVM to call C/C++ libraries and to be called by C/C++ libraries which may be specific to hardware.
6. Native Method LibrariesThese are collections of native libraries required for executing native methods. They include libraries written in languages like C and C++.
Note: For more information refer to this YouTube video link: How Java Works?
How JVM Works - JVM Architecture?
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4