Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Java: NoSuchMethodError from dependency conflicts

NoSuchMethodError means the class you compiled against is not on the runtime classpath. Find the duplicate with dependency:tree and enforce one version.

~5 min readJava
#Java#Maven#Classpath#Debugging
A Java classpath crate with two amber JAR versions clashing at a cyan loader gate, a dependency tree beside it

Introduction & Quick Summary

The application compiles, the tests pass, and at runtime the first call into a library fails:

code
Exception in thread "main" java.lang.NoSuchMethodError: 'void com.example.payments.Gateway.charge(java.lang.String, long)'

NoSuchMethodError is thrown by the JVM when a class was linked against a method that does not exist on the class it actually loaded. The compiler resolved the call against one version of Gateway; the runtime classpath contains a different version where that signature is absent. Source code and runtime disagree, and the compiler had no way to see it.

Typical causes:

  • Two versions of the same library on the classpath, with Maven or Gradle mediation selecting the older one.
  • A transitive dependency upgraded a library while another dependency pins it down.
  • A shaded or fat JAR bundles its own copy of a class that also exists at the top level.
  • A dependency is provided at compile time and missing at runtime, or the reverse.
  • An application server's parent-first delegation loads a server copy instead of the packaged one.

Quick answer: find which JAR actually contains the class, compare it with the version compiled against, and remove the duplicate. Do not add the missing JAR blindly; the extra copy is usually the problem.

Core Mechanics, Signatures & Reproducible Setup

The message includes the full signature and return type, which is more information than the stack trace shows at first glance. 'void com.example.payments.Gateway.charge(java.lang.String, long)' names exactly what the caller expects: a virtual method returning void with a String and a primitive long.

java
package com.example.payments; public interface Gateway { void charge(String customerId, long amountCents); } public final class App { public static void main(String[] args) { Gateway gateway = GatewayFactory.load(); gateway.charge("cus_1", 1999); // links against the signature above } }

If the runtime Gateway declares charge(String, long, String currency), or declares charge as static, linkage fails with NoSuchMethodError or IncompatibleClassChangeError. Reproduce by placing two versions of the library on the classpath and running the same main class:

bash
java -cp "app.jar:lib/gateway-1.0.jar:lib/gateway-2.0.jar" com.example.App

The error family narrows the diagnosis:

ErrorMeaning
NoSuchMethodErrormethod missing with that descriptor
NoSuchFieldErrorfield missing or type changed
NoClassDefFoundErrorclass present at compile, absent or failed at runtime
AbstractMethodErrorinterface method not implemented by the loaded class
IncompatibleClassChangeErrorclass versus interface changed, or static versus instance

Key Comparison Table

StrategyFinds the duplicatePrevents recurrenceEffort
mvn dependency:tree -Dverboseyesnolow
Maven Enforcer dependencyConvergenceyesyes, fails the buildlow
Gradle resolutionStrategy or platform BOMpartlyyesmedium
Explicit <exclusions>yesper dependencylow
Shade with relocationyesyes, for fat JARshigh
-verbose:class at runtimeconfirms the loaded JARnolow

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

1. Confirm which JAR the JVM loaded

Guessing from pom.xml is unreliable because the effective classpath includes transitive JARs and container-provided ones. Ask the JVM.

bash
java -verbose:class -cp app.jar com.example.App 2>&1 | grep 'com.example.payments.Gateway' # [Loaded com.example.payments.Gateway from file:/app/lib/gateway-1.0.jar] # or from inside the running process jshell --class-path app.jar <<'EOF' System.out.println(com.example.payments.Gateway.class.getProtectionDomain() .getCodeSource().getLocation()); EOF

The first loaded copy wins, so the order of the classpath matters as much as the versions present.

2. Maven dependency mediation

Maven resolves conflicts by nearest definition: the version closest to the root wins, regardless of which is newer. A direct dependency at depth one beats a transitive newer version at depth three.

bash
mvn -q dependency:tree -Dverbose -Dincludes=com.example:gateway # com.example:gateway:jar:1.0 (version managed from 2.0; omitted for conflict)

Add an exclusion at the dependency that drags in the old version, or manage the version explicitly in dependencyManagement. Then make the build fail when it happens again:

xml
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-enforcer-plugin</artifactId> <executions> <execution> <id>enforce</id> <goals><goal>enforce</goal></goals> <configuration> <rules><dependencyConvergence/></rules> </configuration> </execution> </executions> </plugin>

The rule is strict and will surface unrelated conflicts the first time; that is the point, because those conflicts are the same bug waiting for a different call path.

3. Gradle resolution

Gradle picks the highest version by default, which usually matches expectations but can still select a version that lost a method.

kotlin
configurations.all { resolutionStrategy { force("com.example:gateway:2.0.1") failOnVersionConflict() } }

Prefer a platform BOM when the libraries publish one: it aligns versions across modules and removes per-dependency pinning. failOnVersionConflict() is the Gradle equivalent of convergence enforcement.

4. Shaded and fat JARs

A shaded JAR that bundles a library without relocation ships a second copy of the class. At runtime the first copy on the classpath wins, and the bundled copy is invisible until a method is called.

xml
<plugin> <artifactId>maven-shade-plugin</artifactId> <configuration> <relocation> <pattern>com.example.gateway</pattern> <shadedPattern>internal.shaded.com.example.gateway</shadedPattern> </relocation> </configuration> </plugin>

Relocation keeps both copies usable, because they become different classes. Without it, one of them silently wins.

5. Application servers and Spring Boot layouts

Tomcat, WildFly, and similar servers load their own libraries with parent-first delegation, so a server-provided Gateway hides the one in WEB-INF/lib. Spring Boot's executable JAR uses a nested layout where BOOT-INF/lib is loaded by a custom classloader with its own order.

bash
# Spring Boot fat jar unzip -l app.jar | grep gateway # BOOT-INF/lib/gateway-1.0.jar # extract and inspect the effective classpath java -Djarmode=tools -jar app.jar list 2>/dev/null | head

Align the packaged version with what the server provides, or mark the dependency provided and let the container supply it consistently.

6. Scope mistakes

provided compiles but is absent at runtime; runtime is absent at compile time and present later; test never reaches production. A method added in a version only present in test scope produces a test suite that passes and a deployment that fails.

xml
<dependency> <groupId>com.example</groupId> <artifactId>gateway</artifactId> <version>2.0.1</version> <scope>runtime</scope> </dependency>

Run the packaged artifact once in CI with java -jar and a smoke call. That single step catches scope and layout problems that unit tests cannot see.

Common Compilation/Runtime Mistakes & Version Gotchas

  • Adding the newer JAR without removing the older one. The classpath now has both, and load order decides the winner, which differs between environments.
  • Version ranges. [1.0,2.0) resolves differently over time. Pin exact versions for applications and use ranges only in libraries.
  • Java modules. Split packages across module-info.java boundaries fail at layer resolution; the error names the package rather than the duplicate JAR.
  • Agents and instrumentation. A -javaagent that injects classes can change linkage in ways no dependency tree shows. Check -verbose:class in the failing environment.
  • Mocking frameworks. A mocked interface in tests can mask a signature change until production code calls the real implementation.
SymptomCauseFix
passes in IDE, fails in JARIDE classpath orderrun the packaged artifact in CI
fails only on the app serverparent-first delegationalign provided scope
first request fails, later ones passstatic initializer orderingfix the duplicate, not the order
fails after a dependency bumpmediation changeddependency:tree, converge

Compare two pom.xml or lockfile revisions with the text diff tool to see which version moved, and use the regex tester to pull the relevant lines out of a long dependency tree.

The JVM contract is documented in NoSuchMethodError, and the Maven rule that prevents this class of bug is described under Dependency Convergence.

Official references

primary documentation

The Java documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Java guides

More in Java

All Java guides

The guides filed next to this one in Java, in library order.