Fundamentals 14 min read

Understanding Java Runtime, Compilation, and Class Loading Process

This article explains how Java source files are compiled into bytecode, how the JVM loads and links classes, and details the roles of lexical, syntax, and semantic analysis, as well as the classloader hierarchy and the parent‑delegation mechanism.

New Oriental Technology
New Oriental Technology
New Oriental Technology
Understanding Java Runtime, Compilation, and Class Loading Process

Java Runtime Principle

Java programs run through two steps: compilation of .java source files into .class bytecode, followed by the JVM loading the bytecode into memory (class loading) and interpreting it to produce results.

Java's Execution Model

The JVM provides an abstract virtual machine that runs on any platform. Source code is compiled into platform‑independent bytecode, which the interpreter translates into native machine code for execution.

Java Code Compilation Process

The compilation flow is illustrated by the following diagrams:

Lexical Analyzer

The lexical analyzer tokenizes the input character stream into meaningful lexemes (tokens) and classifies them, without considering syntactic relationships.

Token Stream

The output of lexical analysis is a stream of tokens representing the source code.

Syntax Analyzer

The syntax analyzer checks the token stream against grammar rules and builds a parse tree or abstract syntax tree (AST).

Abstract Syntax Tree (AST)

The AST is a hierarchical representation of the source code structure, where each node corresponds to a language construct.

Semantic Analyzer

The semantic analyzer validates the relationships between constructs, ensuring type correctness and other semantic rules.

Annotated AST

After semantic analysis, the AST is enriched with additional information to form an annotated AST.

Bytecode Generation

The annotated AST is transformed into JVM bytecode, which is written to a .class file.

Class Loading Details

Class loading can be triggered by active references (e.g., accessing static fields, invoking methods, using new , reflection) or passive references (e.g., accessing static constants or creating array elements). The loading process consists of three phases: Loading, Linking, and Initialization.

Loading

Before execution, the JVM loads compiled .class files into memory using class loaders.

Class Loaders

Class loaders read bytecode and create java.lang.Class objects in the method area. The main types are:

Bootstrap ClassLoader (native C++ implementation)

System/Application ClassLoader (loads classes from the classpath)

Extension ClassLoader (loads classes from jre\lib\ext\ )

URLClassLoader (loads classes from specified URLs)

The JVM startup creates a sun.misc.Launcher instance, which in turn creates the Extension and Application class loaders. The Application ClassLoader is used to load user applications.

// Launcher constructor
public Launcher() {
    Launcher.ExtClassLoader var1;
    try {
        // Create extension class loader with null parent
        var1 = Launcher.ExtClassLoader.getExtClassLoader();
    } catch (IOException var10) {
        throw new InternalError("Could not create extension class loader", var10);
    }
    try {
        // Create application class loader with ExtClassLoader as parent
        this.loader = Launcher.AppClassLoader.getAppClassLoader(var1);
    } catch (IOException var9) {
        throw new InternalError("Could not create application class loader", var9);
    }
    Thread.currentThread().setContextClassLoader(this.loader);
    String var2 = System.getProperty("java.security.manager");
    // ... omitted code ...
}

Parent Delegation Model

When a class loader receives a load request, it first delegates the request to its parent. Only if the parent cannot load the class does the child attempt to load it. This ensures security and prevents duplicate loading.

The Application ClassLoader's loadClass method ultimately calls its parent’s loadClass method, implementing the delegation mechanism.

// ClassLoader.loadClass implementation (parent delegation)
protected Class
loadClass(String name, boolean resolve) throws ClassNotFoundException {
    synchronized (getClassLoadingLock(name)) {
        Class
c = findLoadedClass(name);
        if (c == null) {
            long t0 = System.nanoTime();
            try {
                if (parent != null) {
                    c = parent.loadClass(name, false);
                } else {
                    c = findBootstrapClassOrNull(name);
                }
            } catch (ClassNotFoundException e) {
                // ignore
            }
            if (c == null) {
                long t1 = System.nanoTime();
                c = findClass(name);
                // statistics omitted
            }
        }
        if (resolve) {
            resolveClass(c);
        }
        return c;
    }
}

Linking

Linking consists of Verification, Preparation, and Resolution.

Verification

Ensures the class file conforms to JVM specifications.

Preparation

Allocates memory for static fields and assigns default values.

Resolution

Transforms symbolic references in the constant pool into direct references.

Initialization

Executes static initializers and assigns the programmer‑specified initial values to static fields.

Class loading is now complete.

Remarks

When the JVM encounters a new instruction, it checks whether the referenced class has already been loaded; if not, it triggers class loading. After loading, object memory is allocated using either pointer bumping or a free‑list strategy, depending on heap layout and the garbage collector.

javaJVMbytecodeCompilationruntimeprogramming fundamentalsClass Loading
New Oriental Technology
Written by

New Oriental Technology

Practical internet development experience, tech sharing, knowledge consolidation, and forward-thinking insights.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.