Caused by: java.lang.ClassNotFoundException: jakarta.xml.bind.JAXBException – Java

by
Ali Hasan
caching spring spring-boot

Quick Fix: For Java 11 or more, the javax.xml.bind package is removed from the default classpath. Add the necessary dependencies to your pom.xml or build.gradle to resolve the "JAXBException" error.

The Problem:

I’m upgrading a Springboot application from version 2.x to 3.x. After the upgrade, the application fails to start with the following error: java.lang.ClassNotFoundException: jakarta.xml.bind.JAXBException. Can you guide me in resolving this issue?

The Solutions:

Solution 1: Add Required Dependencies

In Spring 3, the javax.xml.bind package is no longer included in the default classpath, which is required for JAXBException. To resolve this issue, add the following dependencies in your pom.xml or build.gradle file:

For Maven:

<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-runtime</artifactId>
    <version>2.3.2</version>
</dependency>

<dependency>
    <groupId>jakarta.xml.bind</groupId>
    <artifactId>jakarta.xml.bind-api</artifactId>
    <version>2.3.2</version>
</dependency>

For Gradle:

implementation 'org.glassfish.jaxb:jaxb-runtime:2.3.2'
implementation 'jakarta.xml.bind:jakarta.xml.bind-api:2.3.2'

By adding these dependencies, you can ensure that the necessary JAXB libraries are available for your application to run successfully.

Q&A

How to fix the issue caused by ClassNotFoundException: jakarta.xml.bind.JAXBException?

Make sure you have JDK 17 or more, and add the dependencies for jaxb-runtime and jakarta.xml.bind-api.

What version should be used for the jaxb-runtime and jakarta.xml.bind-api dependencies?

Version 2.3.2 is recommended.

For which version of Java Spring 3 can be used?

Spring 3 requires at least JDK 17.

Video Explanation:

The following video, titled "How to Fix : java.lang.ClassNotFoundException: javax.xml.bind ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

How to Fix : java.lang.ClassNotFoundException: javax.xml.bind.JAXBException.