BackendBench
Coffee

Home โ€บ Java exceptions โ€บ NoSuchMethodError

Error ยท java.lang

NoSuchMethodError: causes and fixes

Code calls a method that existed at compile time but is missing from the version on the run-time classpath.

What it looks like

java.lang.NoSuchMethodError: 'void com.google.common.base.Preconditions.checkArgument(boolean, java.lang.String, java.lang.Object)'

Extends IncompatibleClassChangeError in java.lang.

What it means

Your code compiled against one version of a library, and at run time a different version is on the classpath that lacks the method with that exact signature. It is the classic symptom of dependency version conflicts, the so-called jar hell: two libraries need different versions of a third, and the build tool picks one. The message includes the full method signature, which tells you which library and which method.

Common causes

How to fix it

  1. Run mvn dependency:tree -Dverbose or gradle dependencyInsight --dependency name to see which versions compete.
  2. Force a single version with dependencyManagement (Maven) or a constraint or resolutionStrategy (Gradle).
  3. Align framework modules by importing the framework BOM.
  4. Print where the class came from: Foo.class.getProtectionDomain().getCodeSource().

Broken and fixed

fails
// compiled against Guava 31, runtime has Guava 20
Preconditions.checkArgument(ok, "bad %s", value);
works
<dependencyManagement>
  <dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>33.2.1-jre</version>
  </dependency>
</dependencyManagement>

๐Ÿ”Ž 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 NoSuchMethodError a checked or unchecked exception?

NoSuchMethodError is an Error (it extends IncompatibleClassChangeError). 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 NoSuchMethodError in?

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

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

Read the message and the first stack trace line in your own code. The usual causes are: Two dependencies pull different versions of the same library and the older one wins; and A BOM or parent POM pins a version that is older than a library expects.

Related exceptions

More tools you might need