Quick revision
The short answer
NoSuchMethodError at runtime means some code was compiled against one version of a class and is running with another version on the classpath. Here the new reporting library most likely pulled in an older Guava that won Maven's mediation. We confirm it with mvn dependency:tree -Dverbose -Dincludes=com.google.guava, fix it by pinning the needed version in dependencyManagement or excluding the unneeded copy, check for duplicate or shaded Guava classes in other jars, and add Enforcer rules so the next conflict fails the build instead of the startup.
What to remember
In this answer, we saw that NoSuchMethodError means code compiled against one version of a class is running with another. We used dependency:tree with -Dverbose and -Dincludes to see that the reporting library's old Guava won mediation, fixed it by pinning the version in dependencyManagement or excluding the copy, looked for hidden Guava classes with banDuplicateClasses, and added requireUpperBoundDeps so the next conflict fails the build.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
NoSuchMethodError at runtime means some code was compiled against one version of a class and is running with another version on the classpath. Here the new reporting library most likely pulled in an older Guava that won Maven's mediation. We confirm it with mvn dependency:tree -Dverbose -Dincludes=com.google.guava, fix it by pinning the needed version in dependencyManagement or excluding the unneeded copy, check for duplicate or shaded Guava classes in other jars, and add Enforcer rules so the next conflict fails the build instead of the startup.
Why this question is asked
This failure has a typical shape: the code compiles, the unit tests pass, and the service dies at startup with an error deep inside a library we never touched. Interviewers ask it because it tests whether we know how the classpath is put together and have a method for finding the culprit, instead of randomly changing versions. In this answer, we will see what NoSuchMethodError really means, find the conflicting Guava versions, fix the problem two ways, look for a less obvious cause in shaded and duplicate classes, and make the build catch this next time.
1What NoSuchMethodError Really Tells Us
When Java code is compiled, every method call records the exact class, method name and parameter types it expects. Nothing is checked again until the JVM runs that line. If the class loaded at runtime does not have that method, because it is an older or newer version of the class, the JVM throws java.lang.NoSuchMethodError, which is an Error, not an Exception, and usually kills the startup.
So the error is not a bug in our code. It is a mismatch between the version some code was compiled against and the version actually on the classpath. Our job is to find which jars bring Guava and which version won.
Notice the stack trace already says a lot: the caller is our own cache library, which was compiled against a recent Guava, and the method it wants is missing, so an old Guava must be on the classpath.
2Finding Which Guava Won
The dependency plugin shows the full tree, and -Dincludes narrows it to the artifact we care about. The -Dverbose flag also shows the versions that lost mediation and why. Recent versions of the dependency plugin support it; check your plugin version if it is ignored.
- Read the stack tracewhich class and method is missing, who calls it
- Run dependency:tree -Dverbose -Dincludeswhich versions are requested and which won
- Older version won?YesPin the needed version or exclude the copyNoLook for duplicate or shaded classes
- Restart and run the tests that use both librariesconfirm the fix
- Add Enforcer rulesthe next conflict fails the build
5 steps in order. Play them, or step through them one at a time.
This is Maven's nearest-wins mediation at work. The reporting library declares Guava 19.0 at depth two, our cache library needs 32.1.3-jre also at depth two, and because reporting-lib was declared first, 19.0 wins the tie. Before we added the reporting library, 32.1.3-jre was the only version and everything worked.
3Fix 1: Pin the Version in dependencyManagement
The cleanest fix is to tell Maven which Guava to use everywhere. Spring Boot's BOM does not manage Guava, so we add it ourselves, choosing the version the newer code needs. Guava keeps strong backward compatibility for non-beta APIs, so the old reporting library will usually work with the newer version, but we must run its code paths to be sure.
If the old library breaks on the new Guava, for example because it uses an API that was removed, we look for a newer version of the reporting library first. Downgrading Guava would just break our cache library instead.
4Fix 2: Exclude the Transitive Copy
If the reporting library does not really need Guava, for example because it only uses it in a feature we do not call, we can exclude it from that path.
But there is a catch: if the library does call Guava on some code path, we have traded a startup failure for a NoClassDefFoundError later. Pinning the version is usually safer, and exclusion is best for things we truly do not want, such as a second logging implementation.
5When the Tree Looks Fine: Duplicate and Shaded Classes
Sometimes the tree shows a single, correct Guava, and the error remains. The usual reason is that the same classes arrive in a different jar. Some libraries bundle a copy of Guava inside their own jar under the original package name, a 'fat' or badly shaded jar, and Maven cannot see it as a Guava dependency. The JVM simply loads whichever com.google.common.collect.ImmutableMap it finds first on the classpath.
The Enforcer rule banDuplicateClasses, from the extra-enforcer-rules project by MojoHaus, scans the jars on the classpath and fails when the same class appears in more than one. Running it once quickly tells us which jar carries the hidden copy. The fix is then a newer version of that library, a properly relocated shaded version, or replacing it.
6Making the Build Catch It Next Time
The startup failure was the first time anyone noticed the conflict. We want the build to notice instead. The Enforcer plugin has two rules for this. requireUpperBoundDeps fails when the version that won is lower than one some dependency asked for, which is exactly our case. dependencyConvergence is stricter and fails whenever two different versions meet at all.
Notice that the build now fails with the same information we had to dig for, at the moment the reporting library is added. Putting the rule in the corporate parent gives every service this protection.
7Follow-up questions
Why did the unit tests not catch it?
Because no unit test exercised the code path that calls the newer Guava method with the full classpath. Unit tests often mock the cache library, and the call happens during Spring startup. A single test that starts the application context, such as a @SpringBootTest smoke test, would have failed in CI, which is a good reason to keep one.
Could you see which jar a class was loaded from at runtime?
Yes. Starting the JVM with -verbose:class prints every loaded class with its source jar, and in code ImmutableMap.class.getProtectionDomain().getCodeSource().getLocation() returns the jar location. Both are useful when the dependency tree and the runtime disagree.
Is dependencyConvergence or requireUpperBoundDeps better?
requireUpperBoundDeps is easier to adopt because it only fails when an older version wins, which is the risky case. dependencyConvergence also fails when a newer version wins, which is safer in theory but produces many failures in a typical Spring Boot service. Many teams start with requireUpperBoundDeps and add exclusions to it for known safe cases.
8Conclusion
In this answer, we saw that NoSuchMethodError means code compiled against one version of a class is running with another. We used dependency:tree with -Dverbose and -Dincludes to see that the reporting library's old Guava won mediation, fixed it by pinning the version in dependencyManagement or excluding the copy, looked for hidden Guava classes with banDuplicateClasses, and added requireUpperBoundDeps so the next conflict fails the build.
Open with
NoSuchMethodError at runtime tells me that some code was compiled against one version of a class and is running with a different version. So my first suspect is the library I just added.
Then, if they want more
I would read the stack trace to see which method is missing and who calls it, then run mvn dependency:tree -Dverbose -Dincludes=com.google.guava. That shows every path that brings Guava, which version won and which were omitted for conflict.
Typically the reporting library brings an old Guava, say 19, at the same or a nearer depth than the library that needs a recent one, and Maven's nearest-wins rule picks the old one.
To fix it, I would pin the needed Guava version in dependencyManagement, since Spring Boot does not manage Guava, and run the reporting features to be sure they work on it. If the reporting library does not really need Guava, I could exclude it, but that risks a NoClassDefFoundError on a code path I did not test.
If the tree looks correct and the error stays, I would look for duplicate or badly shaded Guava classes inside another jar, using the banDuplicateClasses rule from extra-enforcer-rules. Finally, I would add requireUpperBoundDeps, or dependencyConvergence, to the Enforcer configuration, ideally in the corporate parent, so the next conflict fails the build rather than the startup.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Apache Maven guides
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "After adding a reporting library, a Spring Boot service fails at startup with...": https://scrivio-interview-library.netlify.app/java/maven/753d735236/