BackendBench
Coffee

Home โ€บ Java exceptions โ€บ NoClassDefFoundError

Error ยท java.lang

NoClassDefFoundError: causes and fixes

A class that was present at compile time could not be found or initialised at run time.

What it looks like

java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils

Extends LinkageError in java.lang.

What it means

The code compiled against a class, but when the JVM tried to link it at run time the class definition was missing, or its static initialiser had already failed. Unlike ClassNotFoundException it is an Error and it comes from the JVM, not from a lookup you wrote. Two very different problems share this name: a dependency that is missing at run time, and a class whose earlier initialisation threw an exception (look for "Could not initialize class").

Common causes

How to fix it

  1. If the message says "Could not initialize class X", find the earlier ExceptionInInitializerError in the log; that is the real cause.
  2. Otherwise compare the compile and run-time classpath and check the dependency scope.
  3. Run the dependency tree and look for excluded or conflicting versions.
  4. Build one executable jar and test it in a clean environment.

Broken and fixed

fails
<dependency>
  <artifactId>commons-lang3</artifactId>
  <scope>provided</scope> <!-- not on the runtime classpath -->
</dependency>
works
<dependency>
  <artifactId>commons-lang3</artifactId>
  <!-- default compile scope: packaged and available at run time -->
</dependency>

๐Ÿ”Ž Unravel the failing code ๐Ÿ”Ž Unravel the fix

Debug it faster

Paste the whole stack trace into the stack trace beautifier to hide framework frames, jump to the root cause and find the first line of your own code. It runs in your browser, so the trace never leaves your machine.

Frequently asked questions

Is NoClassDefFoundError a checked or unchecked exception?

NoClassDefFoundError is an Error (it extends LinkageError). Errors signal serious problems in the JVM or environment; applications normally should not catch them, and ordinary catch (Exception e) blocks will not.

Which package is NoClassDefFoundError in?

It lives in java.lang, so the import is import java.lang.NoClassDefFoundError; (java.lang needs no import).

What is the first thing to check when I see NoClassDefFoundError?

Read the message and the first stack trace line in your own code. The usual causes are: A dependency present at compile time but missing at run time, often provided scope or a transitive dependency excluded by mistake; and A previous ExceptionInInitializerError: the first use failed and every later use reports NoClassDefFoundError: Could not initialize class.

Related exceptions

More tools you might need