Sub topic 14
Class Identity and Common Loading Errors
Assume you can compile and run basic Java programs and understand classes, methods, static members, and exceptions. This chapter explains why two classes with the same name can still be different types.
Class identity
The JVM identifies a class using both:
- Its binary name, such as
com.example.Plugin. - The class loader that defines it.
The same class bytes loaded by two different class loaders produce two different JVM classes. This rule lets applications isolate libraries that use the same class names.
A class loader that defines a class is called its defining loader. The loader may first ask its parent for the class, but the loader that eventually calls defineClass becomes the defining loader.
import java.nio.file.Files;
import java.nio.file.Path;
public class LoaderIdentityDemo {
public static class Payload {
}
static class ByteArrayLoader extends ClassLoader {
private final byte[] bytes;
ByteArrayLoader(byte[] bytes) {
super(null);
this.bytes = bytes;
}
@Override
protected Class<?> findClass(String name)
throws ClassNotFoundException {
if (!name.equals(Payload.class.getName())) {
throw new ClassNotFoundException(name);
}
return defineClass(name, bytes, 0, bytes.length);
}
}
public static void main(String[] args) throws Exception {
Path file = Path.of("LoaderIdentityDemo$Payload.class");
byte[] bytes = Files.readAllBytes(file);
Class<?> left = new ByteArrayLoader(bytes)
.loadClass(Payload.class.getName());
Class<?> right = new ByteArrayLoader(bytes)
.loadClass(Payload.class.getName());
System.out.println(left == right);
System.out.println(left == Payload.class);
Object value = left.getDeclaredConstructor().newInstance();
try {
Payload payload = (Payload) value;
System.out.println(payload);
} catch (ClassCastException ex) {
System.out.println("The cast failed: " + ex.getClass().getSimpleName());
}
}
}Compile and run this program:
$ javac LoaderIdentityDemo.java
$ java LoaderIdentityDemo
false
false
The cast failed: ClassCastExceptionBoth left and right have the binary name LoaderIdentityDemo$Payload. They are different because separate loader objects defined them. Neither one is the application-loaded Payload.class.
Common loading errors
| Error | Usual meaning | Typical cause |
|---|---|---|
| `ClassNotFoundException` | An explicit class lookup failed | `Class.forName` or `loadClass` could not find the requested class |
| `NoClassDefFoundError` | A needed class could not be defined or linked | A dependency is missing at runtime, or initialization previously failed |
| `ClassCastException` | The source and target types are incompatible | The same class name came from different defining loaders |
ClassNotFoundException is checked, so code that explicitly requests a class must handle or declare it. NoClassDefFoundError is an Error, not a checked exception. It often appears later, when ordinary application code first uses a class whose dependency is unavailable.
A ClassCastException can occur even when error messages show identical class names. Compare the loaders:
System.out.println(object.getClass().getClassLoader());
System.out.println(ExpectedType.class.getClassLoader());A null result means the bootstrap loader defined the class, as with String. Application classes normally report the application class loader. Two non-null loader objects can still be different instances.
Exercise
Exercise
Why does a class loaded from the same byte array by two separate loaders fail a cast to the application-loaded class with the same binary name?
Show answer
Class identity includes the binary name and the defining class loader. The separate loaders create distinct JVM types, so the object is not an instance of the application-loaded class.