Video in progress
The code compiled fine, but at runtime it throws NoSuchMethodError. How can that happen?
The code was compiled against one version of a library, but at runtime a different version ended up on the classpath, and it lacks that method (or that exact signature). The usual cause is a transitive dependency conflict.
📖 Answer
The compiler writes a method call into the bytecode with the name and the full descriptor, for example com/acme/Client.send(Ljava/lang/String;)V. At runtime the JVM looks for exactly that signature. If it can't find it, you get NoSuchMethodError. It is an Error, not an Exception, and it usually appears on the first call.
A typical case:
my-service
├── lib-a 2.1 -> jackson-databind 2.17
└── lib-b 1.4 -> jackson-databind 2.12
Maven picks the "nearest" version, and 2.12 may win. lib-a calls a new method that exists in 2.17 and crashes. Gradle takes the highest version by default, but that can break another library too.
A changed signature causes the same error: the method used to return void and now returns boolean. It is compatible at the source level, but at the bytecode level it is a different method.
How I track it down:
mvn dependency:tree -Dverboseorgradle dependencyInsight --dependency jackson-databind;- to see where a class was loaded from at runtime:
-Xlog:class+load=infoorSomeClass.class.getProtectionDomain().getCodeSource().
The fix: pin a single version through dependencyManagement or the Spring Boot BOM. NoSuchFieldError, AbstractMethodError and IncompatibleClassChangeError come from the same family, and the cause is almost always the same.
Sign up to mark studied questions and save your progress.