Spring Web Service打包JAR运行报NoClassDefFoundError排查咨询
javax/xml/soap/SOAPException NoClassDefFoundError in Spring Web Service JAR Hey there, let's walk through the most likely causes for this error—since your service works in embedded Tomcat but breaks when running the JAR, the issue almost always boils down to missing dependencies in the packaged JAR or Java version-related module changes. Here's where to start:
1. Check Your Java Version & Modular JDK Restrictions
Starting with Java 9, the JDK moved many EE-related APIs (including javax.xml.soap which contains SOAPException) out of the default classpath into optional modules. When running in your IDE, the embedded Tomcat might be pulling in these modules automatically, but when you package to a JAR, the standalone JVM doesn't include them by default.
- If you're using Java 9+, verify you're either:
- Adding the
java.xml.wsmodule via JVM arguments:java --add-modules java.xml.ws -jar your-service.jar(note: this module is deprecated in Java 9+ and removed in Java 11) - Or better yet, explicitly adding the required dependencies to your build file (see next point)
- Adding the
2. Fix Missing SOAP Dependencies in Your Build File
The SOAPException class lives in the javax.xml.soap-api library. Your IDE might be using Tomcat's internal copy of this library, but Spring Boot's executable JAR doesn't include it by default unless you explicitly add it.
For Maven:
Add this dependency to your pom.xml (use the Spring Boot managed version if possible):
<!-- Use Spring Boot's starter for web services to auto-manage dependencies --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> <!-- Or if you need just the SOAP API explicitly --> <dependency> <groupId>javax.xml.soap</groupId> <artifactId>javax.xml.soap-api</artifactId> <version>1.4.0</version> </dependency>
For Gradle:
implementation 'org.springframework.boot:spring-boot-starter-web-services' // Or explicit API dependency implementation 'javax.xml.soap:javax.xml.soap-api:1.4.0'
Critical Note: If you had this dependency marked as provided scope before, change it to compile (Maven) or implementation (Gradle)—provided tells the build tool not to include it in the JAR, which is why it works in IDE (Tomcat provides it) but fails standalone.
3. Verify Spring Boot Packaging Plugin Configuration
Double-check your spring-boot-maven-plugin or spring-boot-gradle-plugin settings to ensure you're not accidentally excluding the SOAP-related dependencies. For example, avoid any <exclusions> blocks that target javax.xml.soap or related modules.
- Run
mvn dependency:tree(Maven) orgradle dependencies(Gradle) to confirm thejavax.xml.soap-apiis present in your dependency tree and marked as a runtime dependency.
4. Check for JAXB Compatibility (Java 11+)
If you're on Java 11 or newer, the JDK completely removed JAXB and JAX-WS APIs. In addition to the javax.xml.soap-api, you might need to add the JAXB implementation dependencies:
Maven:
<dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-core</artifactId> <version>2.3.0.1</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>2.3.3</version> </dependency>
Gradle:
implementation 'com.sun.xml.bind:jaxb-core:2.3.0.1' implementation 'com.sun.xml.bind:jaxb-impl:2.3.3'
5. Test the Packaged JAR Locally
After updating dependencies, rebuild your JAR with mvn clean package (Maven) or gradle clean bootJar (Gradle), then run it again with java -jar your-service.jar. This should resolve the missing class error if the dependency was the issue.
内容的提问来源于stack exchange,提问作者grangos

