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

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

To resolve the java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException, you need to know your project structure, and according to it, you can add the jar files or dependencies to your project.

In this blog, we will discuss how to fix java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException

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.

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

What is jaxb-api Artifact?

The jaxb-api stands for Java Architecture for XML Binding 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.

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

Why does java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException occurs?

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 will not be able 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 JAXB module at runtime using the –add-modules java.xml.bind option.

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.

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

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

1. For Maven projects

For Maven projects, you can add the dependency in the pom.xml file. Add the following dependency.

<dependencies>
  <dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.1</version> <!-- or newer -->
  </dependency>

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

  <dependency>
    <groupId>javax.activation</groupId>
    <artifactId>activation</artifactId>
    <version>1.1.1</version>
  </dependency>
</dependencies>

By adding the above dependencies, the error will be resolved.

2. For Non-Maven 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. For Gradle Projects

When using Gradle, add the following dependencies to your build.gradle file.

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. For 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

5. For 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.

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

6. For 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.

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

To learn more about this topic, you can refer to our Java Course.

Conclusion

To fix the java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException, you need to add JAXB to your project. If you use Maven or Gradle, add the needed dependencies. If you don’t use a build tool, download the JAR files and add them yourself. For Java 9 or 10, use –add-modules java.xml.bind when running your program. Also, for SBT projects, you can add the jakarta.xml.bind-api dependency in the build.sbt file, and for Ivy projects, include it in the ivy.xml file.

Get 100% Hike!

Master Most in Demand Skills Now!

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 does JAXB work?

Jakarta XML Binding (JAXB; formerly Java Architecture for XML Binding) is a software framework that allows Java EE developers to map Java classes to XML representations.

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.

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