How to Resolve java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException

How to Resolve java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException

If you’re seeing the error java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException while working with XML in Java, especially in Java 11 or later, you’re not alone. Starting from Java 11, JAXB is no longer included in the JDK, which causes this NoClassDefFoundError in Java. This guide will show you how to fix JAXBException in Java 11+ by adding the correct XML Binding (JAXB) API dependencies for Java 11 and Java 17, depending on your project setup. In this blog, you will learn about Maven and how to resolve JAXBException in Java without Maven with examples.

Table of Contents:

What is a java.lang.NoClassDefFoundError?

java.lang.NoClassDefFoundError is an error that occurs when a class is found at compile time but not found at runtime. It usually occurs when a required .class file or JAR is not in the classpath.

The error java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException can be broken down into 2 parts, java.lang.NoClassDefFoundError and javax/xml/bind/JAXBException.

  • java.lang.NoClassDefFoundError shows that the JVM cannot find the class at runtime.
  • javax/xml/bind/JAXBException refers to the JAXBException class that could not be found.

The javax/xml/bind/JAXBException issue occurs in Java 9 and above. According to your code, it is using JAXB (Java Architecture for XML Binding), but it is not available in the classpath anymore. This is why you’re seeing javax.xml.bind not found.

What is jaxb-api Artifact?

The jaxb-api stands for Java Architecture for XML Binding (JAXB) API. It allows you to easily convert Java objects into the XML format and convert XML data back into Java objects.

The process of converting Java objects into XML is called marshalling, and the process of converting XML into Java objects is called unmarshalling. To fix JAXB error in Java, understanding this API is crucial.

It is useful where you need to work with XML data, for example, reading or writing to XML files.

Why does javax.xml.bind.JAXBException Error Occurs in Java 9, 10, and 11+?

This error occurs when a Java class is available at compile time, but it is not available at runtime. Up to Java 8, JAXB was included in the JDK. Starting with Java 9, JAXB was moved to a separate module (java.xml.bind) that is not resolved by default.

From Java 11 and later, JAXB was completely removed from the JDK, due to which Java code containing JAXB may compile successfully but can fail at runtime, when the JVM is unable to locate the JAXBException class.

To solve this issue, users have to add the JAXB dependencies to their project by using a Maven or Gradle, or adding a .jar file. If you’re using Java 9 or 10, you can include the java.xml.bind module at runtime using the –add-modules java.xml.bind option. This is how you fix JAXB error in Java for these versions.

Example:

Java

Output:

why_does_noclassdeffounderror_javax_xml_bind_jaxbexception_occur

Explanation:

In the above Java program, the JAXBException dependencies have not been included in the project, due to which the program is throwing the JAXBException. This is the basic NoClassDefFoundError in Java.

Master Java Today - Accelerate Your Future
Enroll Now and Transform Your Future
quiz-icon

Version Compatibility of Java

Java Version Is JAXB Included by Default? Required Action
Java 8 Yes, JAXB is included in the JDK. No additional setup is needed. You can use JAXB classes directly.
Java 9–10 JAXB is available, but not included by default. You need to explicitly add the module using the --add-modules java.xml.bind option during compilation and runtime.
Java 11 and above JAXB is completely removed from the JDK. You must manually add XML Binding (JAXB) API dependencies to your project using a build tool or by including the necessary JAR files.

How to Fix javax.xml.bind.JAXBException Error in Java

1. Fix JAXB Error in Maven Projects

For Maven projects, you can add the JAXB dependencies for Maven in the pom.xml file. This is the primary way to fix JAXB error in Java when using Maven. Thus, add the following dependency.

<dependencies>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>

By adding the above JAXB dependencies for Maven, the JAXB not found error will be resolved. This also helps to fix JAXBException Java 11 issues.

Working Example with Maven

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class Main {
public static void main(String[] args) {
try {
JAXBContext context = JAXBContext.newInstance(Person.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(new Person(“John”, 25), System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
}

static class Person {
public String name;
public int age;

// Default constructor needed for JAXB
public Person() {}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
}

@XmlRootElement
public class User {
private String username;
private String email;

public User() {} // Required for JAXB

public User(String username, String email) {
this.username = username;
this.email = email;
}

// getters and setters
}

Output:

resolve JAXBException working Maven example

Explanation: This JAXB example with Maven shows how to marshal a simple Java object into XML, making JAXB usage easier to understand for beginners. This demonstrates how to fix JAXB error in Java with a working setup.

2. Fix JAXB Error in Non-Maven Java Projects

For non-Maven projects, you need to download the following JARs:

  • jaxb-api-2.3.1.jar
  • jaxb-runtime-2.3.1.jar
  • javax.activation-1.1.1.jar

Then, add the jar files to your project directory via the command line or manually.

For the command line, use the following command:

java -cp ".:path/to/jaxb-api.jar:path/to/jaxb-runtime.jar:path/to/activation.jar" YourClass

3. Error Fix for Gradle Projects

When using Gradle, add the following dependencies to your build.gradle file. These JAXB dependencies for Maven (or rather, Gradle equivalents) help fix JAXBException Java 11.

dependencies {
    implementation 'javax.xml.bind:jaxb-api:2.3.1'
    implementation 'org.glassfish.jaxb:jaxb-runtime:2.3.1'
    implementation 'javax.activation:activation:1.1.1'
}
Unlock Your Future in Java
Start Your Java Journey for Free Today
quiz-icon

4. How to Handle JAXB in Java 9 and 10

JAXB is a part of Java 9 and 10, but is not added by default. You can use the command on the CLI to add it.

javac --add-modules java.xml.bind YourClass.java
java --add-modules java.xml.bind YourClass

This method helps you to fix JAXB error in Java for these specific versions, preventing the JAXB not found error.

5. How to Add JAXB in SBT Projects

Scala Build Tool is a build tool used for Scala and Java Projects. It is used in compiling and testing the code. 

For SBT Projects, you can add the dependency in the build.sbt file. Add the following dependency. This helps to fix JAXB error in Java for SBT projects.

libraryDependencies += "jakarta.xml.bind" % "jakarta.xml.bind-api" % "3.0.1"

6. How to Add JAXB in IVY Projects

IVY is a dependency tool used with Apache Ant; it can also be integrated with other build systems like Gradle. It helps to manage and control the JAR files (or other artifacts) that a Java project relies on.

For IVY Projects, you can add the dependency in the ivy.xml file. Add the following dependency. This helps to fix JAXB error in Java for IVY projects.

<dependency org="jakarta.xml.bind" name="jakarta.xml.bind-api" rev="3.0.1" />

Get 100% Hike!

Master Most in Demand Skills Now!

Real XML Mapping Use Case

  1. Banking and Financial Transactions: Banks exchange transactional data with regulatory bodies and financial partners using XML formats like SWIFT and ISO 20022. XML Binding (JAXB) API mapping ensures that internal banking data conforms to these standards, facilitating smooth international transfers and compliance reporting.
  2. Supply Chain Management: Manufacturers and suppliers use different enterprise resource planning (ERP) systems. XML Binding (JAXB) API mapping allows seamless data exchange, enabling automatic order processing, shipment tracking, and inventory management across different platforms. Without correctly configured JAXB dependencies for Maven or other build tools, these integrations can fail due to a JAXB not found error.
  3. E-Governance and Tax Filing: Government agencies require businesses to submit tax filings and legal documents in standardized XML formats. XML Binding (JAXB) API mapping helps convert company-specific financial records into the required government format, ensuring compliance with regulations such as GST or VAT filings.

Conclusion

To fix JAXBException in Java 11 and beyond, you need to understand how JAXB support changed after Java 8. This blog provided solutions for adding the correct XML Binding (JAXB) API dependency for Java 11 and Java 17, along with setup instructions for Maven, Gradle, and manual methods. Even if you’re not using a build tool, you can still resolve JAXBException in Java without Maven by downloading and adding the required JAR files manually. With the included version compatibility table and working JAXB examples, you’re now equipped to handle this error confidently.

To learn more about this topic, you can refer to our Java Course and also check out the Java Interview Questions prepared by industry experts.

How to resolve java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException – FAQs

Q1. Can I still use JAXB in Java 17?

Yes, JAXB is well-supported in Java and works seamlessly with Java 17.

Q2. What is the use of JAXB?

It provides a runtime binding framework for client applications, including unmarshalling, marshalling, and validation capabilities.

Q3. How to fix NoClassDefFoundError JAXBException in Java 11?

To fix NoClassDefFoundError: JAXBException in Java 11, manually add the jaxb-api, jaxb-core, jaxb-impl, and activation dependencies to your project.

Q4. Is JAXB a framework?

The JAXB framework enables developers to perform the following operations: Unmarshal XML content into a Java representation.

Q5. What are JAXB properties?

JAXB allows the application to specify two sets of properties that modify its behavior: context properties and marshaller properties.

Q6. How to fix JAXBException in Java 17 without Maven?

Fix JAXBException in Java 17 without Maven by manually adding the required JAXB JAR files to your classpath, such as jaxb-api, jaxb-impl, and jaxb-runtime.

Q7. Why does JAXBException not exist in Java 11?

JAXBException does not exist in Java 11 because JAXB was removed from the JDK starting with Java 11 and must be included as an external dependency.

Q8. Can JAXB be used in Spring Boot with Java 17?

JAXB can be used in Spring Boot with Java 17 by adding the necessary JAXB dependencies (javax.xml.bind:jaxb-api, com.sun.xml.bind:jaxb-impl, and org.glassfish.jaxb:jaxb-runtime) to your project.

Q9. What is the difference between JAXB and other XML parsers?

JAXB maps XML directly to Java objects (and vice versa), while other XML parsers (like DOM or SAX) only read or manipulate XML as a tree or stream without object binding.

Q10. How do I check if JAXB is already in my classpath?

Try running java -jar yourapp.jar and see if it throws ClassNotFoundException for javax.xml.bind.*, or check with javap javax.xml.bind.JAXBContext.

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner