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.

Introduction & Quick Summary
The application compiles, the tests pass, and at runtime the first call into a library fails:
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
providedat 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.
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:
java -cp "app.jar:lib/gateway-1.0.jar:lib/gateway-2.0.jar" com.example.AppThe error family narrows the diagnosis:
| Error | Meaning |
|---|---|
NoSuchMethodError | method missing with that descriptor |
NoSuchFieldError | field missing or type changed |
NoClassDefFoundError | class present at compile, absent or failed at runtime |
AbstractMethodError | interface method not implemented by the loaded class |
IncompatibleClassChangeError | class versus interface changed, or static versus instance |
Key Comparison Table
| Strategy | Finds the duplicate | Prevents recurrence | Effort |
|---|---|---|---|
mvn dependency:tree -Dverbose | yes | no | low |
Maven Enforcer dependencyConvergence | yes | yes, fails the build | low |
Gradle resolutionStrategy or platform BOM | partly | yes | medium |
Explicit <exclusions> | yes | per dependency | low |
| Shade with relocation | yes | yes, for fat JARs | high |
-verbose:class at runtime | confirms the loaded JAR | no | low |
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.
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());
EOFThe 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.
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:
<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.
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.
<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.
# 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 | headAlign 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.
<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.javaboundaries fail at layer resolution; the error names the package rather than the duplicate JAR. - Agents and instrumentation. A
-javaagentthat injects classes can change linkage in ways no dependency tree shows. Check-verbose:classin the failing environment. - Mocking frameworks. A mocked interface in tests can mask a signature change until production code calls the real implementation.
| Symptom | Cause | Fix |
|---|---|---|
| passes in IDE, fails in JAR | IDE classpath order | run the packaged artifact in CI |
| fails only on the app server | parent-first delegation | align provided scope |
| first request fails, later ones pass | static initializer ordering | fix the duplicate, not the order |
| fails after a dependency bump | mediation changed | dependency: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 documentationThe Java documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Java guides
All Java guidesSpring Boot: No qualifying bean of type available
The context could not satisfy a constructor argument. Scan boundaries, missing stereotypes, ambiguous candidates and test slices are the usual reasons.
Method Scope in Java: How Local Variables Work
Understand how variables behave inside Java methods: local variable boundaries, block scope, parameter scope, shadowing, and lifetime implications.
Java: HikariPool connection is not available, request timed out
The HikariCP pool ran out of connections for the whole connectionTimeout. Read the counters, find leaks or slow queries, then size the pool.
Java: DateTimeParseException: Text could not be parsed
The formatter pattern and the input disagree. Check the index in the message, the locale, and whether the text needs a ZonedDateTime instead.
More in Java
All Java guidesThe guides filed next to this one in Java, in library order.