DEV Community

mehmet akar
mehmet akar

Posted on

Implementation of jaxb-api has not been found on module path or classpath. Error Solution

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]
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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'
}
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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'
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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's dependencies 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.

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay