---
spec: JVMS
title: "The Java Virtual Machine Specification"
edition: "Java SE 27 Edition"
java_version: 27
chapter: "Chapter 5. Loading, Linking, and Initializing"
section: "5.3.2"
heading: "5.3.2. Loading Using a User-defined Class Loader"
parent: "5.3. Creation and Loading"
source: https://docs.oracle.com/javase/specs/jvms/se27/html/jvms-5.html#jvms-5.3.2
id: "5.3.2"
url: https://jvm.obfuscated.software/jvms/se27/5.3.2.html
raw: https://jvm.obfuscated.software/jvms/se27/5.3.2.md
---

# 5.3.2. Loading Using a User-defined Class Loader

The process of loading and creating the nonarray class or interface C denoted by `N` using a user-defined class loader `L` is as follows.

First, the Java Virtual Machine determines whether `L` has already been recorded as an initiating loader of a class or interface denoted by `N`. If so, this class or interface is C, and no class loading or creation is necessary.

Otherwise, the Java Virtual Machine invokes the `loadClass` method of class `ClassLoader` on `L`, passing the name `N` of a class or interface. `L` must perform one of the following two operations to load and thereby create a class or interface C:

1. The class loader `L` can load C directly. This is accomplished by obtaining an array of bytes that purports to represent C as a `ClassFile` structure ([§4.1](4.1.md)), and then invoking the method `defineClass` of class `ClassLoader`. Invoking `defineClass` causes the Java Virtual Machine to derive a class or interface C denoted by `N` from the array of bytes using `L`, and then to create C, via the algorithm of [§5.3.5](5.3.5.md). `L` should use the result of `defineClass` as the result of `loadClass`.
2. The class loader `L` can load C indirectly, by delegating the loading of C to some other class loader `L`'. This is accomplished by passing the argument `N` to an invocation of a method on `L`' (typically the `loadClass` method of class `ClassLoader`). `L` should use the result of that method as the result of `loadClass`.

The following rules apply regardless of which operation is performed:

- If a class loader cannot find a purported representation of a class or interface denoted by `N`, it must throw a `ClassNotFoundException`. The process of loading and creating C then fails with a `NoClassDefFoundError` whose cause is the `ClassNotFoundException`.
- If a class loader finds a purported representation of C, but deriving C from the purported representation fails, then the process of loading and creating C fails for the same reason.
- If a class loader throws an exception other than a `ClassNotFoundException`, then the process of loading and creating C fails for the same reason.

If the invocation of `loadClass` on `L` has a result, then:

- If the result is `null`, or the result is a class or interface with a name other than `N`, then the result is discarded, and the process of loading and creation fails with a `NoClassDefFoundError`.
- Otherwise, the result is the created class or interface C. The Java Virtual Machine records that `L` is an initiating loader of C ([§5.3.4](5.3.4.md)). The process of loading and creating C succeeds.

Since JDK 1.1, Oracle’s Java Virtual Machine implementation has invoked the one-argument `loadClass` method on a class loader to cause it to load a class or interface. The argument to `loadClass` is the name of the class or interface to be loaded. There is also a two-argument version of the `loadClass` method, where the second argument is a `boolean` that indicates whether the class or interface is to be linked or not. Only the two-argument version was supplied in JDK 1.0.2, and Oracle’s Java Virtual Machine implementation relied on it to link the loaded class or interface. From JDK 1.1 onward, Oracle’s Java Virtual Machine implementation links the class or interface directly, without relying on the class loader.
