Java XML Tutorial

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

Migrate a Java project to Java 11 and hits the below "class not found error for JAXBException"


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

A short history of JAXB on Java
The Jakarta XML Binding (JAXB; formerly Java Architecture for XML Binding) is an XML binding framework to convert Java classes to and from XML.

  1. The JAXB is part of Java 6, 7, and 8.
  2. Java 9 deprecated the Java EE modules, including the JAXB packages javax.xml.*.
  3. The JAXB APIs are still deprecated in Java 10.
  4. Java 11 deleted the JAXB APIs javax.xml.* completely.
  5. Oracle submitted the Java EE to Eclipse Foundation and repackaged it to jakarta.xml.* since version 3.0.

Solution

To run JAXB on Java 9 and above, we have to include the JAXB API and RI dependencies manually.

There are two standard JAXB implementations; choose either one will solve the above "class not found error for JAXBException".

Jakarta XML Binding

Since JAXB version 3 and above, the package is changed from javax.xml.* to jakarta.xml.*.

pom.xml

  <!-- JAXB API only -->
  <dependency>
      <groupId>jakarta.xml.bind</groupId>
      <artifactId>jakarta.xml.bind-api</artifactId>
      <version>3.0.0</version>
  </dependency>

  <!-- JAXB RI, Jakarta XML Binding -->
  <dependency>
      <groupId>com.sun.xml.bind</groupId>
      <artifactId>jaxb-impl</artifactId>
      <version>3.0.0</version>
      <scope>runtime</scope>
  </dependency>

If we still prefer the old JAXB packages javax.xml.*, stick with the JAXB version 2.x.

pom.xml

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

  <dependency>
      <groupId>com.sun.xml.bind</groupId>
      <artifactId>jaxb-ri</artifactId>
      <version>2.3.3</version>
  </dependency>
pom.xml

  <!-- JAXB API only -->
  <dependency>
      <groupId>jakarta.xml.bind</groupId>
      <artifactId>jakarta.xml.bind-api</artifactId>
      <version>3.0.0</version>
  </dependency>

  <!-- JAXB RI, EclipseLink MOXy -->
  <dependency>
      <groupId>org.eclipse.persistence</groupId>
      <artifactId>org.eclipse.persistence.moxy</artifactId>
      <version>3.0.0</version>
  </dependency>

Download Source Code

$ git clone https://github.com/mkyong/core-java

$ cd java-xml

$ cd src/main/java/com/mkyong/xml/jaxb/

References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
5 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Andreu
2 years ago

I have the same problem with javax/xml/bind/DatatypeConverter I added both dependencies but still get the same java.lang.NoClassDefFoundError. Any suggestions?

I’m using ‘org.springframework.boot’ version ‘2.6.6’ and Java 11

CarlosCarreno
1 year ago
Reply to  Andreu

for JDK 11 you must add this maven dependecy

<!– API, java.xml.bind module –>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>2.3.2</version>
</dependency>

<!– Runtime, com.sun.xml.bind module –>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.2</version>
</dependency>

reference: https://stackoverflow.com/questions/43574426/how-to-resolve-java-lang-noclassdeffounderror-javax-xml-bind-jaxbexception

Konstantin
1 year ago
Reply to  Andreu

Try adding:

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

Alex
1 year ago

Hello,

I try to migrate to spring boot 3.0 and I updated my dependencies as follow but I still have an error with ValidationEventHandler:

jakarta.xml.bind
jakarta.xml.bind-api
3.0.0

com.sun.xml.bind
jaxb-impl
3.0.0
runtime

Caused by: java.lang.ClassNotFoundException: javax.xml.bind.ValidationEventHandler

I tried to add this dependency also:

javax.xml.bind
jaxb-api
2.3.1

But having the error described here: https://mkyong.com/java/java-lang-classnotfoundexception-com-sun-xml-bind-v2-contextfactory/

Any idea are welcome!
Thanks for your help!

Alekhya
1 year ago

Hello sir, I am facing this issue once after migrating to Java11 and gradle 7.5.1 also upgrading cxf plugin version to 4.0.0
I have added the above mentioned dependencies but still the error persists