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:
- The class loader
Lcan load C directly. This is accomplished by obtaining an array of bytes that purports to represent C as aClassFilestructure (§4.1), and then invoking the methoddefineClassof classClassLoader. InvokingdefineClasscauses the Java Virtual Machine to derive a class or interface C denoted byNfrom the array of bytes usingL, and then to create C, via the algorithm of §5.3.5.Lshould use the result ofdefineClassas the result ofloadClass. - The class loader
Lcan load C indirectly, by delegating the loading of C to some other class loaderL'. This is accomplished by passing the argumentNto an invocation of a method onL' (typically theloadClassmethod of classClassLoader).Lshould use the result of that method as the result ofloadClass.
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 aClassNotFoundException. The process of loading and creating C then fails with aNoClassDefFoundErrorwhose cause is theClassNotFoundException. - 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 thanN, then the result is discarded, and the process of loading and creation fails with aNoClassDefFoundError. - Otherwise, the result is the created class or interface C. The Java Virtual Machine records that
Lis an initiating loader of C (§5.3.4). 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.