With the evolution of Java, certain dependencies like Jakarta XML Binding (JAXB) have undergone changes, leading to issues like java.lang.NoClassDefFoundError
or org.springframework.beans.factory.BeanCreationException: ... jakarta/xml/bind/JAXBException
This error commonly arises when migrating Java projects to Java 11 or later versions due to the removal of JAXB APIs in Java 11. This post provides insights into resolving this error by including JAXB API and RI dependencies manually.
Background
Java 9 deprecated the Java EE modules, including JAXB packages like javax.xml.*
. Subsequently, Java 11 removed these JAXB APIs entirely. To address this issue, Oracle submitted Java EE to the Eclipse Foundation, resulting in a repackaging of JAXB as jakarta.xml.*
since version 3.0.
Solution
To resolve the java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
or org.springframework.beans.factory.BeanCreationException
errors, manual inclusion of JAXB API and RI dependencies is necessary.
Two standard JAXB implementations are available: Jakarta XML Binding and EclipseLink MOXy.
Jakarta XML Binding
For projects requiring JAXB version 3.x and above, the following dependencies should be included in the pom.xml
file:
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>3.0.0</version>
<scope>runtime</scope>
</dependency>
For projects preferring the old JAXB packages javax.xml.*
, sticking with version 2.x is recommended:
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-ri</artifactId>
<version>2.3.3</version>
</dependency>
EclipseLink MOXy
For EclipseLink MOXy, include the following dependencies:
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.moxy</artifactId>
<version>3.0.0</version>
</dependency>
Additional Notes
- Ensure proper dependency management according to the project's requirements.
- Verify compatibility with Spring Boot versions and other dependencies.
- Refer to official documentation or community forums for further assistance on specific issues encountered during migration.
I hope you enjoyed reading this, and I'm curious to hear if this tutorial helped you. Please let me know your thoughts below in the comments. Don't forget to subscribe to my newsletter to avoid missing my upcoming blog posts.
You can also find me here LinkedIn • Twitter • GitHub or Medium
Conclusion
By manually including the necessary dependencies, such as JAXB API and RI, the java.lang.NoClassDefFoundError
related to javax.xml.bind.JAXBException
can be effectively resolved, enabling seamless migration and execution of Java projects on versions 11 and above.