Quick revision
The short answer
Two dependencies need different Guava versions, and the build put only one on the classpath, an older one that lacks the ImmutableMap.of overload the other library was compiled against. Maven picks the version nearest to our project in the dependency tree, Gradle picks the highest by default, so adding a library can silently change which Guava we get. We find the culprit with mvn dependency:tree or gradle dependencyInsight, pin one compatible version through dependencyManagement or a Gradle platform using the Guava BOM, make sure we have the -jre flavour rather than -android, and add a convergence check to CI.
What to remember
In this answer, we saw that NoSuchMethodError means a library was compiled against a Guava with a method that the Guava on our runtime classpath lacks. Maven's nearest-wins and Gradle's highest-wins rules decide which version we get, so a new dependency can change it silently. We found the conflict with mvn dependency:tree or gradle dependencyInsight, confirmed the loaded jar at runtime, pinned one compatible -jre version through dependencyManagement or a Gradle platform with the Guava BOM, and added Enforcer convergence rules or Gradle conflict failures so the next conflict breaks the build instead of startup.
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.
Two dependencies need different Guava versions, and the build put only one on the classpath, an older one that lacks the ImmutableMap.of overload the other library was compiled against. Maven picks the version nearest to our project in the dependency tree, Gradle picks the highest by default, so adding a library can silently change which Guava we get. We find the culprit with mvn dependency:tree or gradle dependencyInsight, pin one compatible version through dependencyManagement or a Gradle platform using the Guava BOM, make sure we have the -jre flavour rather than -android, and add a convergence check to CI.
Why this question is asked
NoSuchMethodError is one of the most confusing errors in Java because the code compiled perfectly. It does not mean our code is wrong; it means a class at runtime is a different version from the one some code was compiled against. Guava is the classic example, since almost every library depends on it. Interviewers ask this debugging question to see whether we understand how build tools resolve transitive dependencies and how to take control of them. In this answer, we will see what the error really means, how Maven and Gradle choose a version, how to find the conflict, how to fix it with a BOM, the -jre and -android trap, and how to stop it from coming back.
1What NoSuchMethodError Really Means
When the Java compiler compiles a call such as ImmutableMap.of(k1, v1, ..., k6, v6), it records the exact method signature in the calling class's bytecode. At runtime, the JVM looks for that exact signature in whatever ImmutableMap class is on the classpath. If the class exists but that method does not, the JVM throws NoSuchMethodError when the call is first linked, which is often at startup.
So the error tells us that some library, here the other dependency, was compiled against a Guava that has this method, and the Guava on our runtime classpath does not. Guava's ImmutableMap.of has gained overloads for more key-value pairs over time, so a library built against a recent Guava can call an overload that an older Guava simply does not have (check the release notes for the exact version that added it).
2How Maven and Gradle Choose One Version
A classpath can only have one version of a class, so when two dependencies ask for different Guava versions, the build tool must pick one. Maven uses nearest-wins: it chooses the version declared closest to our project in the dependency tree, and among equal depths, the one declared first. Gradle by default uses highest-wins: it picks the highest requested version.
That explains why adding a library can break another. Suppose library A needs Guava 33 and sits two levels deep, while the new library B declares Guava 28 directly as its dependency, one level deep. Maven now picks Guava 28 because it is nearer, and A fails at runtime. In Gradle, the same situation picks 33, which usually works, but a library that needs an older API removed in a newer version could fail the other way.
- our-servicethe project
- library-b 2.1 (direct)declares guava 28.2-jre: depth 1
- library-a 5.0 (direct)depends on lib-core
- lib-core 5.0declares guava 33.x-jre: depth 2
- Result: guava 28.2-jre winsnearest to the project; A calls a missing method
5 layers, from the top down. Step through them one at a time.
Maven chooses by distance, not by version number.
3Finding the Conflict
We first ask the build tool what it resolved and why. In Maven, dependency:tree with an includes filter shows every path to Guava; the verbose option also shows the versions that were omitted because of conflicts. In Gradle, dependencyInsight explains, for one configuration, which version was selected and which requests lost.
The last line is worth remembering. It shows the jar the running JVM actually loaded the class from, which also catches cases the build tree cannot show, such as a fat jar or a shaded library that contains its own copy of Guava classes.
4Fixing It With a Pinned Version and the BOM
The fix is to decide the Guava version ourselves instead of letting distance or chance decide. We choose a version that satisfies every library, usually the newest one they need, since Guava keeps strong backward compatibility for non-beta APIs, and pin it. In Maven, versions in dependencyManagement override every transitive request; importing the Guava BOM (com.google.guava:guava-bom) manages the Guava artifacts together. In Gradle, a platform does the same.
After the change we run the dependency tree again to confirm a single Guava, and start the service to confirm the error is gone. If two libraries truly need incompatible Guava versions, which is rare, the remaining options are upgrading or downgrading one library, or a library that shades its own copy of Guava.
5The -jre and -android Flavours
Guava is published in two flavours with the same artifact name and different version suffixes: 33.x-jre for server Java and 33.x-android for Android and older Java. The android flavour leaves out some APIs that need newer Java features, such as some methods that use java.util.stream or java.time. If the resolution ends up with the android flavour on a server, perhaps because one library declared it, we can get NoSuchMethodError even when the version number looks right.
Recent Guava versions publish Gradle module metadata that helps Gradle pick the right flavour for the platform, but Maven simply compares version strings. So when we pin the version, we pin the -jre flavour explicitly and check the resolved version string in the tree.
6Catching Conflicts in CI
The real lesson is that dependency conflicts should fail the build, not production. In Maven, the Enforcer plugin's dependencyConvergence rule fails the build when different versions of the same dependency are requested and not resolved by our own management, and requireUpperBoundDeps fails when the chosen version is lower than one that a dependency asked for, which is exactly our case. In Gradle, failOnVersionConflict() on a configuration, or strict versions in the platform, do the same job.
With these rules in place, adding library B would have failed the pull request with a message naming both Guava versions, long before startup in any environment.
7Follow-up questions
Why did the code compile if the method does not exist?
Our own code does not call the method; library A does, and A was compiled long ago against its own Guava. Our compile only checks our calls against the classpath we compile with. The mismatch appears only when the JVM links A's call at runtime.
Does Spring Boot manage the Guava version for us?
Spring Boot's dependency management covers many libraries, but Guava is not among the versions it manages, as far as recent releases go (check your Boot version's managed dependencies). So in Spring Boot projects we still pin Guava ourselves, ideally in a company BOM.
What other errors come from the same cause?
NoClassDefFoundError and ClassNotFoundException when a class was removed or moved, AbstractMethodError and IncompatibleClassChangeError when interfaces changed between versions. All of them mean the runtime version differs from the compile-time version of some code, and the investigation is the same.
8Conclusion
In this answer, we saw that NoSuchMethodError means a library was compiled against a Guava with a method that the Guava on our runtime classpath lacks. Maven's nearest-wins and Gradle's highest-wins rules decide which version we get, so a new dependency can change it silently. We found the conflict with mvn dependency:tree or gradle dependencyInsight, confirmed the loaded jar at runtime, pinned one compatible -jre version through dependencyManagement or a Gradle platform with the Guava BOM, and added Enforcer convergence rules or Gradle conflict failures so the next conflict breaks the build instead of startup.
Open with
NoSuchMethodError means some code was compiled against a version of a class that has this method, but the class on the runtime classpath doesn't. Here the other library was compiled against a newer Guava with that ImmutableMap.of overload, and adding the new library changed which Guava we resolve.
Then, if they want more
Maven uses nearest-wins, so a library that declares an old Guava one level deep beats a newer one two levels deep; Gradle uses highest-wins by default.
I'd confirm it with mvn dependency:tree -Dincludes=com.google.guava with verbose, or gradle dependencyInsight on the runtime classpath, and at runtime I can print the code source location of ImmutableMap to see which jar was really loaded, which also catches fat or shaded jars.
The fix is to choose the version myself: pin a version that works for every library, usually the newest one needed, in dependencyManagement or a Gradle platform, ideally by importing the Guava BOM. I'd make sure it's the -jre flavour, because the -android flavour lacks some APIs and gives the same error.
Finally I'd add the Maven Enforcer dependencyConvergence and requireUpperBoundDeps rules, or failOnVersionConflict in Gradle, so this fails in CI next time.
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 official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
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 new library, a service fails at startup with java.lang.NoSuchMethodError...": https://scrivio-interview-library.netlify.app/java/utility-libraries/cc71c57280/