Diagnosing NoSuchMethodError and NoSuchFieldError in Spring Boot Jar Packages
This article explains why a Spring Boot application that runs fine in an IDE may throw NoSuchMethodError or NoSuchFieldError when packaged as an executable jar, and provides systematic steps—including JVM class‑loading flags, Maven enforcer rules, and class‑path inspection—to locate and resolve the root cause.
When a Spring Boot project runs correctly in the IDE but throws NoSuchMethodError or NoSuchFieldError after being packaged as an executable jar, the issue is usually related to class loading conflicts.
How to locate the problem : Use the Java class‑loading mechanism to determine which version of the class is actually loaded at runtime. Adding JVM logging parameters such as -verbose:class , -XX:+TraceClassLoading (for older JDKs), or -Xlog:class+load=debug (JDK 17) will list every class loaded and the jar it originates from.
These flags can be added to the application start command, and the output will reveal duplicate or unexpected class definitions.
Tools like Arthas (using the jad command) can decompile loaded classes to inspect their source.
Typical causes :
Duplicate classes in different jars (same package and class name but different implementations).
Transitive Maven dependencies that bring multiple versions of the same library; Maven’s breadth‑first conflict resolution may select a version that is incompatible with the code.
Incorrect use of reflection or mismatched field/method signatures.
Preventive measures during compilation : If the project uses Maven, enable the banDuplicateClasses rule from extra-enforcer-rules to fail the build when duplicate classes are detected. Example configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.4.1</version>
<executions>
<execution>
<id>enforce</id>
<configuration>
<rules>
<dependencyConvergence/>
</rules>
</configuration>
<goals>
<goal>enforce</goal>
</goals>
</execution>
<execution>
<id>enforce-ban-duplicate-classes</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<banDuplicateClasses>
<ignoreClasses>
<ignoreClass>javax.*</ignoreClass>
<ignoreClass>org.junit.*</ignoreClass>
<ignoreClass>net.sf.cglib.*</ignoreClass>
<ignoreClass>org.apache.commons.logging.*</ignoreClass>
<ignoreClass>org.springframework.remoting.rmi.RmiInvocationHandler</ignoreClass>
</ignoreClasses>
<findAllDuplicates>true</findAllDuplicates>
</banDuplicateClasses>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>extra-enforcer-rules</artifactId>
<version>1.0-beta-6</version>
</dependency>
</dependencies>
</plugin>For Gradle/Android Studio projects, similar duplicate‑class issues can appear, but there is currently no automatic detection for reflection‑related problems.
Summary : By inspecting class‑loading logs, checking Maven dependency trees, and using build‑time enforcement rules, developers can identify and eliminate the root causes of NoSuchMethodError and NoSuchFieldError in Spring Boot jar deployments.
Cognitive Technology Team
Cognitive Technology Team regularly delivers the latest IT news, original content, programming tutorials and experience sharing, with daily perks awaiting you.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.