When working with Java applications that utilize Java Architecture for XML Binding (JAXB), you might encounter the following error:
javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath.
- with linked exception:
[java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory]
This error typically arises due to the absence of JAXB implementation classes in your project's classpath, especially when running on Java versions 9 and above.
Understanding the Issue
Starting with Java 9, the JAXB API was deprecated and removed from the default classpath. By Java 11, it was completely removed from the JDK. As a result, applications relying on JAXB need to explicitly include the necessary JAXB libraries in their project setup.
Resolving the Exception
To resolve this issue, you need to include the appropriate JAXB dependencies in your project. The exact steps depend on your build tool and project setup.
1. For Maven Projects
Add the following dependencies to your pom.xml
file:
<dependencies>
<!-- JAXB API -->
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>3.0.0</version>
</dependency>
<!-- JAXB Runtime Implementation -->
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
These dependencies include the JAXB API and its runtime implementation, ensuring that your application has access to the necessary classes during runtime.
2. For Gradle Projects
Include the following in your build.gradle
file:
dependencies {
// JAXB API
implementation 'jakarta.xml.bind:jakarta.xml.bind-api:3.0.0'
// JAXB Runtime Implementation
implementation 'org.glassfish.jaxb:jaxb-runtime:3.0.0'
}
This configuration will resolve the JAXBException
by adding the required JAXB libraries to your project.
Alternative Implementations
If you prefer to use an alternative JAXB implementation, such as EclipseLink MOXy, you can include its dependencies instead:
Maven:
<dependencies>
<!-- JAXB API -->
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>3.0.0</version>
</dependency>
<!-- EclipseLink MOXy Implementation -->
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.moxy</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
Gradle:
dependencies {
// JAXB API
implementation 'jakarta.xml.bind:jakarta.xml.bind-api:3.0.0'
// EclipseLink MOXy Implementation
implementation 'org.eclipse.persistence:org.eclipse.persistence.moxy:3.0.0'
}
Configuration for MOXy
When using EclipseLink MOXy, you must specify the JAXBContextFactory
to use its implementation. Create a jaxb.properties
file in the same package as your domain classes with the following content:
jakarta.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
This configuration directs JAXB to use EclipseLink MOXy as its implementation.
General Debugging Tips
- Check Java Version: Ensure you are aware of your project's Java version and its compatibility with the included JAXB libraries.
- Use Dependency Management Tools: Use tools like Maven's
dependency:tree
or Gradle'sdependencies
task to confirm that the correct versions of JAXB libraries are included. - Classpath Issues: Verify that your classpath includes the necessary JAXB libraries during runtime.
Conclusion
The "Implementation of JAXB-API has not been found" error is a common issue when migrating to newer Java versions. By explicitly including the required JAXB dependencies and ensuring proper configuration, you can resolve this issue and ensure compatibility with modern Java environments.
Author Of article : mehmet akar Read full article