javax.xml.bind.JAXBException Implementation of JAXB-API has not been found on module path or classpath
Asked Answered
T

13

152

I'm trying to run my Spring Boot application on Java 9, and I've faced with JAXB problem, which described in the guides, but didn't work for me. I've added dependency on JAXB api, and application started working. If you get the following exception, as a result of missing JAXB missing implementation using Java version >=9:

javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath.
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:177) ~[jaxb-api-2.3.0.jar:2.3.0]
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:364) ~[jaxb-api-2.3.0.jar:2.3.0]
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:508) ~[jaxb-api-2.3.0.jar:2.3.0]
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:465) ~[jaxb-api-2.3.0.jar:2.3.0]
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:366) ~[jaxb-api-2.3.0.jar:2.3.0]
    at com.sun.jersey.server.impl.wadl.WadlApplicationContextImpl.<init>(WadlApplicationContextImpl.java:107) ~[jersey-server-1.19.1.jar:1.19.1]
    at com.sun.jersey.server.impl.wadl.WadlFactory.init(WadlFactory.java:100) [jersey-server-1.19.1.jar:1.19.1]
    at com.sun.jersey.server.impl.application.RootResourceUriRules.initWadl(RootResourceUriRules.java:169) [jersey-server-1.19.1.jar:1.19.1]
    at com.sun.jersey.server.impl.application.RootResourceUriRules.<init>(RootResourceUriRules.java:106) [jersey-server-1.19.1.jar:1.19.1]
    at com.sun.jersey.server.impl.application.WebApplicationImpl._initiate(WebApplicationImpl.java:1359) [jersey-server-1.19.1.jar:1.19.1]
    at com.sun.jersey.server.impl.application.WebApplicationImpl.access$700(WebApplicationImpl.java:180) [jersey-server-1.19.1.jar:1.19.1]
    at com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:799) [jersey-server-1.19.1.jar:1.19.1]
    at com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:795) [jersey-server-1.19.1.jar:1.19.1]
    at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193) [jersey-core-1.19.1.jar:1.19.1]
    at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:795) [jersey-server-1.19.1.jar:1.19.1]
    at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:790) [jersey-server-1.19.1.jar:1.19.1]
    at com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:509) [jersey-servlet-1.19.1.jar:1.19.1]
    at com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:339) [jersey-servlet-1.19.1.jar:1.19.1]
    at com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:605) [jersey-servlet-1.19.1.jar:1.19.1]
    at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:207) [jersey-servlet-1.19.1.jar:1.19.1]
    at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:394) [jersey-servlet-1.19.1.jar:1.19.1]
    at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:744) [jersey-servlet-1.19.1.jar:1.19.1]
    at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:270) [tomcat-embed-core-9.0.10.jar:9.0.10]
    at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:106) [tomcat-embed-core-9.0.10.jar:9.0.10]
    at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4491) [tomcat-embed-core-9.0.10.jar:9.0.10]
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5135) [tomcat-embed-core-9.0.10.jar:9.0.10]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) [tomcat-embed-core-9.0.10.jar:9.0.10]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1427) [tomcat-embed-core-9.0.10.jar:9.0.10]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1417) [tomcat-embed-core-9.0.10.jar:9.0.10]
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) [na:na]
    at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75) [tomcat-embed-core-9.0.10.jar:9.0.10]
    at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:140) [na:na]
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:943) [tomcat-embed-core-9.0.10.jar:9.0.10]
    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:839) [tomcat-embed-core-9.0.10.jar:9.0.10]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) [tomcat-embed-core-9.0.10.jar:9.0.10]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1427) [tomcat-embed-core-9.0.10.jar:9.0.10]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1417) [tomcat-embed-core-9.0.10.jar:9.0.10]
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) [na:na]
    at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75) [tomcat-embed-core-9.0.10.jar:9.0.10]
    at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:140) [na:na]
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:943) [tomcat-embed-core-9.0.10.jar:9.0.10]
    at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:258) [tomcat-embed-core-9.0.10.jar:9.0.10]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) [tomcat-embed-core-9.0.10.jar:9.0.10]
    at org.apache.catalina.core.StandardService.startInternal(StandardService.java:422) [tomcat-embed-core-9.0.10.jar:9.0.10]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) [tomcat-embed-core-9.0.10.jar:9.0.10]
    at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:770) [tomcat-embed-core-9.0.10.jar:9.0.10]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) [tomcat-embed-core-9.0.10.jar:9.0.10]
    at org.apache.catalina.startup.Tomcat.start(Tomcat.java:371) [tomcat-embed-core-9.0.10.jar:9.0.10]
    at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.initialize(TomcatWebServer.java:107) [spring-boot-2.1.0.M1.jar:2.1.0.M1]
    at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.<init>(TomcatWebServer.java:86) [spring-boot-2.1.0.M1.jar:2.1.0.M1]
    at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getTomcatWebServer(TomcatServletWebServerFactory.java:413) [spring-boot-2.1.0.M1.jar:2.1.0.M1]
    at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:174) [spring-boot-2.1.0.M1.jar:2.1.0.M1]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:179) [spring-boot-2.1.0.M1.jar:2.1.0.M1]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:152) [spring-boot-2.1.0.M1.jar:2.1.0.M1]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) [spring-context-5.1.0.RC1.jar:5.1.0.RC1]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) [spring-boot-2.1.0.M1.jar:2.1.0.M1]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:769) [spring-boot-2.1.0.M1.jar:2.1.0.M1]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:405) [spring-boot-2.1.0.M1.jar:2.1.0.M1]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:334) [spring-boot-2.1.0.M1.jar:2.1.0.M1]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1252) [spring-boot-2.1.0.M1.jar:2.1.0.M1]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1240) [spring-boot-2.1.0.M1.jar:2.1.0.M1]
    at io.eureka.server.EurekaServerApp.main(EurekaServerApp.java:21) [classes/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.1.0.M1.jar:2.1.0.M1]
Caused by: java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory
    at org.springframework.boot.web.embedded.tomcat.TomcatEmbeddedWebappClassLoader.loadClass(TomcatEmbeddedWebappClassLoader.java:70) ~[spring-boot-2.1.0.M1.jar:2.1.0.M1]
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1138) ~[tomcat-embed-core-9.0.10.jar:9.0.10]
    at javax.xml.bind.ServiceLoaderUtil.nullSafeLoadClass(ServiceLoaderUtil.java:122) ~[jaxb-api-2.3.0.jar:2.3.0]
    at javax.xml.bind.ServiceLoaderUtil.safeLoadClass(ServiceLoaderUtil.java:155) ~[jaxb-api-2.3.0.jar:2.3.0]
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:174) ~[jaxb-api-2.3.0.jar:2.3.0]
    ... 66 common frames omitted
Tomika answered 19/8, 2018 at 9:21 Comment(3)
Marking duplicate as the answer https://mcmap.net/q/22046/-replacements-for-deprecated-jpms-modules-with-java-ee-apis is same as what you've posted.Indoor
@nullpointer This question is not a duplicate. When people run into issues they google the stacktrace. In my case, I posted the entire stacktrace in the question, which proves to be helpful to people when investigating such exceptions. The solution might have been the same, but the questions, context, stactrace and the exceptions addressed are different. If the original question had bundled or quoted exceptions to expect as signs to apply this kind of solution, it would have been helpful. But no, that isn't the case, that's why the solution has got some votes up regardless of duplicate label.Tomika
Do this in a profile: <profiles> <profile> <id>java11</id> <activation> <jdk>[11,)</jdk> </activation> <dependencies> <dependency> <groupId>org.glassfish.jaxb</groupId> <artifactId>jaxb-runtime</artifactId> </dependency> </dependencies> </profile> </profiles>Scalable
T
271

Add these dependencies into your pom/gradle:

Gradle:

compile('javax.xml.bind:jaxb-api:2.3.0')
compile('javax.activation:activation:1.1')
compile('org.glassfish.jaxb:jaxb-runtime:2.3.0')

Pom:

<!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->
<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.0-b170201.1204</version>
</dependency>

<!-- https://mvnrepository.com/artifact/javax.activation/activation -->
<dependency>
    <groupId>javax.activation</groupId>
    <artifactId>activation</artifactId>
    <version>1.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.glassfish.jaxb/jaxb-runtime -->
<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-runtime</artifactId>
    <version>2.3.0-b170127.1453</version>
</dependency>
Tomika answered 19/8, 2018 at 9:21 Comment(9)
Jaxb is included in spring-boot-data-jpa 2.1.0 mvnrepository.com/artifact/org.springframework.boot/…Lording
This worked for me but I see this warning: Illegal reflective access by com.sun.xml.bind.v2.runtime.reflect.opt.Injector ... to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int)Euraeurasia
Strange... When I run the app jar from terminal the error doesn't occur. It occurs only when I run the app from Intellij IDEA.Overweigh
Note: this is a deprecated fix which also implies Illegal reflective access error. Look at Jakarta EE 9 solution here: https://mcmap.net/q/92463/-java-11-package-javax-xml-bind-does-not-exist-duplicate.Singly
You can actually use this 2.3.0, not necessarily the minor versionWart
Just in case someone stumbles upon this while doing the Spring Boot guide on consuming SOAP services (as I did): You'll have to also add these dependencies on the server, see github.com/spring-guides/gs-consuming-web-service/issues/…Jamison
For me, I was trying out many JAXB implementations, which was causing issues.Soandso
Driven me banana's this problem...until I did the above and voila, it worked. Why? I have absolutely no clue...Generated a Kotlin/Springboot project from Spring Initializer=> Gradle+Kotlin+SpringBoot v2.6.4...using IJ Ultimate, and no matter what I did in the Gradle build file, I could not get the test module, to be able to see the JAXB implementation until I did the above. Thank you and good night.Antirachitic
It works for Java 11 without <groupId>javax.activation</groupId>.Felicitous
S
38

Adding the following dependencies fixed the issue in my case:

    <!-- JAXB API -->
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.1</version>
    </dependency>

    <!-- JAXB RI -->
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.3.4</version>
    </dependency>
Surfeit answered 2/8, 2022 at 9:35 Comment(1)
Thanks! The most popular answer did not work for me on Java 11 but this one did.Fredel
B
28

For those seeing this issue more recently with IntelliJ and Java 17

<dependency>
    <groupId>jakarta.xml.bind</groupId>
    <artifactId>jakarta.xml.bind-api</artifactId>
    <version>4.0.0</version>
</dependency>
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>4.0.0</version>
    <scope>runtime</scope>
</dependency>
Bloater answered 19/8, 2022 at 19:14 Comment(0)
U
9

Since I ran into this exact issue, and this is the first SO question that pops up on Google, I'll add my solution since I spent a good 8 hours trying to resolve the issue and couldn't find the answer here.

Some context: We're using Java 17, Spring boot 3.0.2. In our pom we have

        <dependency>
            <groupId>jakarta.xml.bind</groupId>
            <artifactId>jakarta.xml.bind-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
        </dependency>

Since we let Spring manage the versions we get

org.glassfish.jaxb:jaxb-runtime:jar:4.0.1
jakarta.xml.bind:jakarta.xml.bind-api:jar:4.0.0

We use the glassfish implementation, so our ClassCastException was slightly different java.lang.classnotfoundexception: "org.glassfish.jaxb.runtime.v2.contextfactory"

In our case, the issue was that we had wrapped the unmarshal call in a CompletableFuture, which leads to class loader issues related to how the ForkJoinPool works. Once we understood that, we found quite a lot of related issues, like this one.

The solution for us was to pass a classloader as a second argument when creating the JAXBContext, like so:

JAXBContext jaxbContext =
                  JAXBContext.newInstance(MyClass.class.getPackageName(), MyClass.class.getClassLoader());

This finally resolved the issue

Urfa answered 2/6, 2023 at 19:18 Comment(1)
This solution also works when using jaxb from an Eclipse plugin.Cockahoop
C
3

I've run into this today for another reason.
The app was supposed to run on Java 8 but I was running it on Java 11, so yeah check that you're running the proper version of Java.

Commend answered 26/7, 2022 at 9:44 Comment(0)
K
1

I had the same problem with a very different root cause. After adding all the possible combination of libraries without success I discovered that, as stated in this question, executing the REST client within a custom ForkJoinPool affected the class loading process.

Other people seems to have suffered from the same problem (1, 2)

Ku answered 3/6, 2022 at 9:21 Comment(2)
Instead of a ForkJoin, is it possible to maybe reconsider your design? While putting the identified limitation into consideration as a workaround.Tomika
Using parallel streams solved my problem. It seems that previous developer tried to implement a custom thread pool like here baeldung.com/java-8-parallel-streams-custom-threadpoolKu
S
1

it really depends on what library you are using. When using org.glassfish.jaxb this is what worked for me:

    compile 'org.glassfish.jaxb:jaxb-xjc:4.0.3'
    compile 'org.glassfish.jaxb:jaxb-runtime:4.0.3'
    compile 'org.glassfish.jaxb:jaxb-core:4.0.3'
Spirituous answered 15/6, 2023 at 8:17 Comment(0)
G
0

the solution that worked with me is changing the java version to java 8

Giveaway answered 31/8, 2022 at 9:46 Comment(2)
I would not recommend downgrading the version of Java. It's like growing backwards into the past & not into the future.Tomika
@Tomika it doesn't matter. We got what we wanted. For my worked 8 jdkRichellericher
T
0

This worked for me.

<dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.3.3</version>
        <scope>runtime</scope>
</dependency>
    
<dependency>
        <groupId>jakarta.xml.bind</groupId>
        <artifactId>jakarta.xml.bind-api</artifactId>
        <version>2.3.3</version>
</dependency>
Torso answered 21/8, 2023 at 14:24 Comment(1)
Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?Bouzoun
E
0

For those using extra repositories, don't forget to add the Maven Central repository

    <repositories>
        <repository>
            <id>central</id>
            <url>https://repo1.maven.org/maven2/</url>
        </repository>
        <repository>
    </repositories>

Then, add the dependencies

               <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.3.3</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>jakarta.xml.bind</groupId>
            <artifactId>jakarta.xml.bind-api</artifactId>
            <version>2.3.3</version>
        </dependency>
Eisenstein answered 25/9, 2023 at 6:39 Comment(0)
C
0

In my case, I added these below dependencies to get rid off this problem:

    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.3.4</version>
    </dependency>
Crop answered 21/12, 2023 at 10:24 Comment(0)
I
0

Below is my set of dependencies to achieve a successful result

plugins {
  id("org.springframework.boot") version "3.2.0"
  id("io.spring.dependency-management") version "1.1.4"
  ...
}

dependencies {
  xjc("org.glassfish.jaxb:jaxb-xjc:4.0.4")
  xjc("com.sun.xml.bind:jaxb-impl:4.0.4")
  implementation("org.glassfish.jaxb:jaxb-runtime:4.0.4")
  ...
}

openjdk version "21.0.2" 2024-01-16

Intensity answered 1/3, 2024 at 9:23 Comment(0)
L
-1

I should you to use this dependencies if you are using Spring boot like a RestAPI

<dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.3.3</version>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>jakarta.xml.bind</groupId>
        <artifactId>jakarta.xml.bind-api</artifactId>
        <version>2.3.3</version>
    </dependency>
Loosing answered 3/1, 2024 at 12:15 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewWiebmer

© 2022 - 2025 — McMap. All rights reserved.