Java / JVM Architecture (Java21) Interview questions
What happens when a NoClassDefFoundError occurs versus ClassNotFoundException?
These sound similar but represent different failure moments in the class lifecycle.
| ClassNotFoundException | NoClassDefFoundError |
| Checked exception | Unchecked Error |
Thrown by dynamic loading calls like Class.forName() or loadClass() when the class truly cannot be found | Thrown by the JVM when a class was available and referenced at compile time, but is missing or failed to initialize when actually needed at runtime |
| Typically means a classpath was never correct for that lookup | Often means the classpath changed between compile and run, or a class's static initializer threw previously and left it unusable |
A common cause of NoClassDefFoundError is a class whose static initializer failed once - after that, the JVM marks it unusable and throws this error on every subsequent reference, even though the class file itself is still present.
More Related questions...