Where does a class come from at runtime, and what is the difference between ClassNotFoundException and NoClassDefFoundError?
Loaders delegate to their parent before searching themselves, so the JDK always wins a name clash. ClassNotFoundException means a loader was asked for bytes it could not find; NoClassDefFoundError means the JVM needed a class that was there at compile time, or one whose static initialiser had already failed.
Answer
Short answer
ClassLoader delegation means a loader asks its parent first, so class identity depends on both the fully qualified class name and the defining loader. ClassNotFoundException means loading failed when requested, while NoClassDefFoundError usually means a class that was present at compile time cannot be resolved or initialized at runtime.
Was this answer helpful?
Likely follow-ups
- Two modules load the same class file and a cast between them fails. What is happening?
- Why does reading a static final int constant not initialise the class that declares it?
- Which of Class.forName and loadClass runs the static initialiser?
- Why do repeated hot redeploys exhaust Metaspace rather than the heap?
Related questions
- You move a service from Java 11 to Java 21 and it fails at startup with InaccessibleObjectException. What changed, and what do you do about it?hardAlso on jpms5 min
- Your test lifts conversion, but something else moved the wrong way. What are guardrail metrics, and when do they stop a launch?hardSame kind of round: concept5 min
- A method with REQUIRES_NEW was meant to save an audit row even when the main transaction fails. When does it work, and when does it deadlock?hardSame kind of round: concept5 min
- What does a forward deployed engineer interview test that a standard software engineer loop does not?mediumSame kind of round: concept5 min