ClassNotFoundException for javax.xml.bind.JAXBException with Spring Boot when switch to Java 9

You need to add the JAXB dependency (as not provided any longer by default in Java 9) and you have to use Spring Boot 2 :

<dependency>
    <groupId>javax.xml.bind</groupId>
     <artifactId>jaxb-api</artifactId>
    <version>2.3.0</version>
 </dependency>

Note that if you use Java 10, you would have exactly the same issue as the JAXB dependency removal was not done just for the Java 9 version.


The Spring Boot wiki about Java 9 and above lists things that you need to know to run Spring Boot apps on Java 9 and above.

Spring Boot version requirements

  • Spring Boot 1 doesn't support it (and no planned to).
  • Spring Boot 2 supports it.

Spring Boot 2 is the first version to support Java 9 (Java 8 is also supported). If you are using 1.5 and wish to use Java 9 you should upgrade to 2.0 as we have no plans to support Java 9 on Spring Boot 1.5.x.

Java 10 is supported as of Spring Boot 2.0.1.RELEASE while Java 11 is supported as of Spring Boot 2.1.0.M2.

Some known workarounds

AspectJ

With Java 9, if you need to weave classes from the JDK, you need to use AspectJ 1.9. Spring AOP should work fine in most cases with AspectJ 1.8 (the default in Spring Boot 2.0).

JAXB

When upgrading you may face the following:

java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException

Hibernate typically requires JAXB that’s no longer provided by default. You can add the java.xml.bind module to restore this functionality with Java9 or Java10 (even if the module is deprecated).

<dependency>
    <groupId>javax.xml.bind</groupId>
     <artifactId>jaxb-api</artifactId>
    <version>2.3.0</version>
 </dependency>

As of Java11, the module is not available so your only option is to add the JAXB RI (you can do that as of Java9 in place of adding the java.xml.bind module:

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

Lombok

If you are using lombok, the managed version of Spring Boot may not work with the latest JDK. Check the Lombok web site and override its version if necessary.

Some known limitations

These libraries do not have full support for Java 9 yet:

  • Apache Cassandra, see #10453

Please, don't hesitate to edit this post if changes occur about the Java 9 and above compatibility with Spring Boot.


Added below dependency and resolve the issue

<dependency>
    <groupId>javax.xml.bind</groupId>
     <artifactId>jaxb-api</artifactId>
    <version>2.3.0</version>
 </dependency>

Add below maven dependencies in you pom.xml and the issue will get resolve. in Java9/10 JaxB modules has been removed, hence need to add manually.

<dependency>
  <groupId>javax.xml.bind</groupId>
  <artifactId>jaxb-api</artifactId>
  <version>2.2.11</version>
</dependency>
<dependency>
  <groupId>com.sun.xml.bind</groupId>
  <artifactId>jaxb-core</artifactId>
 <version>2.2.11</version>
</dependency>
<dependency>
  <groupId>com.sun.xml.bind</groupId>
  <artifactId>jaxb-impl</artifactId>
  <version>2.2.11</version>
 </dependency>
<dependency>
  <groupId>javax.activation</groupId>
  <artifactId>activation</artifactId>
  <version>1.1.1</version>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.dataformat</groupId>
  <artifactId>jackson-dataformat-xml</artifactId>
</dependency>