NoClassDefFoundError, explained and fixed

NoClassDefFoundError means the JVM needed a class, the code referencing it compiled fine (so the class existed at build time), but at runtime the class loader cannot find it, or found it and failed to initialize it. The message names the missing class in internal form, for example com/example/Widget. Two root causes account for nearly all cases: the jar containing the class is not on the runtime classpath, or the class's static initializer threw an exception the first time the class was touched.

How to find the cause

  1. Convert the name: replace / with . to get the real class name, then identify which library provides it and whether that jar is in the deployed artifact (inside the fat jar, the lib folder, the webapp's WEB-INF/lib, or the module path).
  2. Scroll for an earlier exception: if the true cause is a failed static initializer, the log contains an ExceptionInInitializerError with the real exception the first time the class was used; every later attempt reports NoClassDefFoundError for the same class. Find the first failure, it is the informative one.
  3. Compare build and runtime classpaths: a dependency marked provided in Maven, excluded by a Gradle configuration, or present in the IDE but not packaged, produces exactly this pattern: compiles, runs in the IDE, fails when deployed.

The fixes, in order

  1. Ship the dependency: correct the build so the jar is packaged (remove an incorrect provided scope, include the module in the assembly or shade configuration), then verify by listing the built artifact's contents for the class file itself.
  2. Fix the static initializer when the log shows ExceptionInInitializerError: the nested cause (a missing config file, a failed native load, a null property) is the real bug; the class was present all along.
  3. Align versions when the class moved: a class present in version 1 of a library and renamed or removed in version 2, with different parts of the application built against different versions, fails this way; force one version in the build.
  4. Check classloader boundaries in containers and app servers: a class visible to the server's loader but not the application's (or the reverse) fails identically; place shared libraries where the framework documentation specifies.

Common questions

What is the difference between NoClassDefFoundError and ClassNotFoundException? ClassNotFoundException is a checked exception thrown when code asks for a class by name (Class.forName, driver loading) and it is absent. NoClassDefFoundError is an error raised by the JVM when a class referenced directly by compiled code cannot be loaded. Same underlying absence, different doorway, and they have separate fixes pages because the code paths differ.

Why does it work in my IDE but not from the jar? The IDE builds a classpath from every project dependency; the packaged artifact contains only what the build configuration includes. The difference between those two lists is where the missing jar sits.

The class is in the jar, I checked. What now? Then suspect the static initializer or a classloader split: search the log from the first occurrence for ExceptionInInitializerError, and check whether two loaders each loaded their own copy of a related class.

Related errors

ClassNotFoundException · Could not find or load main class · Could not create the Java Virtual Machine