Scrivio Interview library
ConceptFoundations5 min read · updated 12 November 2026

What is the difference between ClassNotFoundException and NoClassDefFoundError? Give a typical cause of each.

ClassNotFoundException is a checked exception thrown when code asks for a class by name, with Class.forName or ClassLoader.loadClass, and no loader can find it; a typical cause is a JDBC driver class name configured while the driver jar is missing. NoClassDefFoundError is an Error thrown when the JVM needs a class that the code was compiled against and cannot load it at run time, typically a dependency missing from the run-time class path, such as one declared with provided scope, or a class whose static initialiser failed earlier. In both cases the first occurrence and its Caused by line tell the real story.

Why this question is asked

These two look like the same problem with different names, and both appear in logs as some class not found. They come from different situations, though, and confusing them sends an investigation in the wrong direction. Interviewers ask this because the difference shows whether we understand how the JVM links classes at run time. In this answer, we will recap where each one comes from in the type hierarchy, see when each is thrown with code, go through typical causes, and finish with how to read them in logs.

1A Quick Recap: Exception Versus Error

In Java, exceptions are split into checked exceptions, which the compiler forces us to catch or declare, and errors, which signal problems an application normally should not try to handle. ClassNotFoundException is a checked exception, a subclass of ReflectiveOperationException. NoClassDefFoundError is an Error, a subclass of LinkageError, the family of errors the JVM throws when linking classes together fails.

That difference in category reflects the situations. ClassNotFoundException comes from code that explicitly asks for a class by name, a string, and must be ready for the answer no. NoClassDefFoundError comes from the JVM itself, while running code that was compiled against a class that should be there.

The two side by side
AspectClassNotFoundExceptionNoClassDefFoundError
Typechecked exceptionError (LinkageError)
Thrown byClass.forName, loadClass, findClassthe JVM while linking or running code
Meansno loader found a class named in a stringa compiled-against class is unavailable
Typical causedriver or plugin jar missingmissing run-time dependency, failed static init
Message shapecom.acme.Driver (dots)com/acme/Helper (slashes), or Could not initialize

2When ClassNotFoundException Is Thrown

Code that loads a class by name, from configuration or by convention, can only find out at run time whether the class exists.

Java
try {
Class<?> c = Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println(e); // java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
}

The class name is just a string, so the compiler cannot check it. The typical cause is a configured name with its jar missing, such as a JDBC driver class in a configuration file while the driver is not on the class path, or a plugin listed in configuration but not installed. A misspelled name gives the same exception. Frameworks often catch it and rethrow with more context, but the original exception is in the Caused by chain.

3When NoClassDefFoundError Is Thrown

Now consider code that refers to a class directly, so javac checked it at compile time.

Java
// compiled with commons-text on the class path (for example scope provided)
public class Report {
String render(String s) {
return org.apache.commons.text.StringEscapeUtils.escapeHtml4(s);
}
}
new Report().render("<b>");
// java.lang.NoClassDefFoundError: org/apache/commons/text/StringEscapeUtils
// at Report.render(Report.java:4)
// Caused by: java.lang.ClassNotFoundException: org.apache.commons.text.StringEscapeUtils

The JVM resolves the reference to StringEscapeUtils the first time render runs. The class loader cannot find it, so the JVM throws NoClassDefFoundError, with the class name in slash form as it appears in the class file, and usually the loader's ClassNotFoundException as the cause. Notice that the error appears when the code first runs, not at start-up, which is why a missing dependency can hide for days in a rarely used path.

Typical causes are a dependency available when compiling but not at run time, for example declared with Maven's provided scope when nothing in production provides it, a jar left out of the deployment, or a class excluded by shading. The second common cause is different: the class was found but its static initialiser failed on an earlier use, and later uses throw NoClassDefFoundError: Could not initialize class, with the real cause in the first ExceptionInInitializerError.

4Reading Them in Logs

With both, the first occurrence and its Caused by chain matter most. For ClassNotFoundException, the name tells us what was asked for, so we check the configuration and the class path for that jar. For NoClassDefFoundError, we first look at the message: a plain class name means it was missing at run time, so we compare the run-time class path with the compile-time dependencies; Could not initialize class means we should search backwards in the logs for the first ExceptionInInitializerError. Since later occurrences often have shorter messages or less context, sorting by time and reading the earliest one saves a lot of guessing.

5Follow-up questions

Why does NoClassDefFoundError often have a ClassNotFoundException as its cause?

Because the JVM resolves the missing reference by asking the class loader to load the class, and the loader reports failure by throwing ClassNotFoundException. The JVM then wraps that into NoClassDefFoundError, since from the JVM's point of view a class the code depends on is unavailable. So the cause confirms the class was simply not found, as opposed to the Could not initialize case.

Should we catch NoClassDefFoundError?

Generally no, because it means the deployment is broken and the right fix is the class path or the failing initialiser. The narrow exception is optional integration code, for example a library that checks whether an optional dependency is present; even there, checking with Class.forName and catching ClassNotFoundException is the cleaner way.

6Conclusion

In this answer, we saw that ClassNotFoundException is a checked exception from code that loads a class by name, thrown when no loader can find it, typically a configured driver or plugin whose jar is missing. NoClassDefFoundError is a LinkageError the JVM throws when a class the code was compiled against cannot be loaded at run time, typically a dependency missing from the run-time class path or a class whose static initialiser failed earlier. In both cases, the first occurrence in the logs and its Caused by chain point to the cause.

What to say in the interviewAbout 90 seconds aloud

Open with

ClassNotFoundException is a checked exception, a ReflectiveOperationException, thrown when code asks for a class by name, with Class.forName or ClassLoader.loadClass, and no loader can find it. Because the name is just a string, the compiler cannot check it, so the code has to handle the failure.

Then, if they want more

The typical cause is a configured class name whose jar is missing, like a JDBC driver class name in the configuration while the driver jar is not on the class path, or a typo in the name.

NoClassDefFoundError is an Error, a LinkageError, thrown by the JVM when code that was compiled against a class tries to use it and the class cannot be loaded at run time. It shows the name with slashes and usually has a ClassNotFoundException as its cause, and it appears the first time that code path runs, not at start-up.

The typical cause is a dependency that was there when compiling but not at run time, for example provided scope when nothing provides it, or a jar missing from the deployment. The other common cause is a class whose static initialiser failed earlier: then the message says Could not initialize class, and the real cause is in the first ExceptionInInitializerError.

In both cases I look for the first occurrence in the logs and read its Caused by chain.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is the difference between ClassNotFoundException and NoClassDefFoundError? Give a...": https://scrivio-interview-library.netlify.app/java/class-loading/8d5e309d8c/