Class loaders require the cooperation of the Java Virtual Machine to derive and create a class or interface from a binary representation provided by the loader (§5.3.1, §5.3.2). The following steps are used to derive a class or interface C denoted by N from a purported representation in class file format when requested by a class loader L.
-
First, the Java Virtual Machine determines whether the request by class loader
Lto derive a class or interface denoted byNis permitted.If
Lhas already been recorded as an initiating loader of a class or interface denoted byN, derivation throws aLinkageError.If the Java Virtual Machine is already in the process of deriving a class or interface denoted by
Nas requested by class loaderL, derivation throws aClassCircularityError. -
Next, the Java Virtual Machine attempts to parse the purported representation. The purported representation may not in fact be a valid representation of C, so derivation must detect the following problems:
-
If the purported representation is not a
ClassFilestructure (§4.1, §4.8), derivation throws aClassFormatError. -
Otherwise, if the purported representation is not of a supported major or minor version (§4.1), derivation throws an
UnsupportedClassVersionError.UnsupportedClassVersionError, a subclass ofClassFormatError, was introduced in JDK 1.2 to enable easy identification of aClassFormatErrorcaused by an attempt to load a class whose representation uses an unsupported version of theclassfile format. In JDK 1.1 and earlier, an instance ofNoClassDefFoundErrororClassFormatErrorwas thrown in case of an unsupported version, depending on whether the class was being loaded by the system class loader or a user-defined class loader. -
Otherwise, if the purported representation does not actually represent a class or interface named
N, derivation throws aNoClassDefFoundError.This occurs when the purported representation has either a
this_classitem which specifies a name other thanN, or anaccess_flagsitem which has theACC_MODULEflag set.
-
-
If C has a direct superclass, the symbolic reference from C to its direct superclass is resolved using the algorithm of §5.4.3.1, with
Lacting as the defining loader of C. Note that if C is an interface it must haveObjectas its direct superclass, which must already have been loaded. OnlyObjecthas no direct superclass.Any exception that can be thrown as a result of failure of class or interface resolution can be thrown as a result of derivation. In addition, derivation must detect the following problems:
-
If the class or interface named as the direct superclass of C is in fact an interface or a
finalclass, derivation throws anIncompatibleClassChangeError. -
Otherwise, if the class named as the direct superclass of C has a
PermittedSubclassesattribute (§4.7.31) and any of the following is true, derivation throws anIncompatibleClassChangeError:- The superclass is in a different run-time module than C (§5.3.6).
- C does not have its
ACC_PUBLICflag set (§4.1) and the superclass is in a different run-time package than C (§5.3). - No entry in the
classesarray of the superclass'sPermittedSubclassesattribute refers to a class or interface with the nameN.
-
Otherwise, if C is a class and some instance method declared in C can override (§5.4.5) a
finalinstance method declared in a superclass of C, derivation throws anIncompatibleClassChangeError.
-
If C has any direct superinterfaces, the symbolic references from C to its direct superinterfaces are resolved using the algorithm of §5.4.3.1, with
Lacting as the defining loader of C.Any exception that can be thrown as a result of failure of class or interface resolution can be thrown as a result of derivation. In addition, derivation must detect the following problems:
-
If any class or interface named as a direct superinterface of C is not in fact an interface, derivation throws an
IncompatibleClassChangeError. -
Otherwise, for each direct superinterface named by C, if the superinterface has a
PermittedSubclassesattribute (§4.7.31) and any of the following is true, derivation throws anIncompatibleClassChangeError:- The superinterface is in a different run-time module than C.
- C does not have its
ACC_PUBLICflag set (§4.1) and the superinterface is in a different run-time package than C. - No entry in the
classesarray of the superinterface'sPermittedSubclassesattribute refers to a class or interface with the nameN.
If no exception is thrown in steps 1-4, then derivation of the class or interface C succeeds. The Java Virtual Machine marks C to have L as its defining loader, records that L is an initiating loader of C (§5.3.4), and creates C in the method area (§2.5.4).
When derivation succeeds, the process of loading and creating C is not complete until every class loader that was involved in loading C (directly or indirectly) returns C as its result. Depending on the behavior of user-defined class loaders, the process of loading and creating C may yet fail (§5.3.2).
If an exception is thrown in steps 1-4, then derivation of the class or interface C fails with that exception.
Requests to derive a class or interface may be made concurrently by class loader code executing in multiple threads, but the derivation process is sequential. The Java Virtual Machine implementation ensures that only one request by a given class loader to derive a class or interface of a given name is processed at a time, while all other such requests wait until the first request is complete.
As specified by the derivation process, if the first request is successful, no subsequent requests will be permitted. The ClassLoader API provides mechanisms to synchronize derivation requests and cache successful results so that redundant derivation requests do not occur.