How to solve java.lang.NoClassDefFoundError: javax/xml/bind Classes

The java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException error (or other javax.xml.bind classes) typically arises due to missing JAXB API in your Classpath or Runtime. Here’s how to resolve it.

Understanding the Issue

In Java 9 onwards, the Java EE APIs, including JAXB (Java Architecture for XML Binding), are not included by default. These APIs were traditionally bundled with Java 6/7/8 but are now separated into distinct modules in Java 9+.

Here is an example Class which reproduces the issue:

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.StringWriter;

@XmlRootElement
public class HelloWorld {
    private String message;

    public HelloWorld() {
    }

    public HelloWorld(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public static void main(String[] args) {
        HelloWorld helloWorld = new HelloWorld("Hello, World!");
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(HelloWorld.class);
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

            StringWriter stringWriter = new StringWriter();
            marshaller.marshal(helloWorld, stringWriter);
            
            String xmlString = stringWriter.toString();
            System.out.println("XML Representation:");
            System.out.println(xmlString);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }
}

If you try to compile the above code with Java 9 or newer, you will get the following Error:

error: package javax.xml.bind does not exist

Fixing the JAXB Issue

The recommended fix is to include in your project the JAXB API and, if your environment does not provide one, also a concrete implementation of it. It is worth mentioning that a temporary workaround is also available for Java 9 and 10 users.

Java 9 and 10 (temporary fix):

The temporary fix for Java 9 and 10 was to use --add-modules java.se.ee to make all Java EE modules available at runtime.

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

However, note that this won’t work in Java 11 as java.se.ee was removed.

Java 11 and newer:

Firstly, in order to compile the above code, you need to provide the XML Bind API. Jakarta EE users will add the following dependenc:

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

However, if your application hasn’t migrated to Jakarta EE yet, then you should use the legacy jaxb-api dependency:

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

If your Java runtime environment contains the concrete implementation of JAXB API, then you don’t need to add anything else to build and run your code.

For example, WildFly application server contains the JAXB implementation in the path: $JBOSS_HOME/modules/system/layers/base/jakarta/xml/bind/api/main

How to solve java.lang.NoClassDefFoundError: javax/xml/bind

On the other hand, if you need to provide yourself the concrete implementation of JAXB API ( as in our main Java Class discussed here), then add one such as Glassfish’s:

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

Conclusion

This article was a step-by-step guide discussing how to understand and fix the issue java.lang.NoClassDefFoundError: javax/xml/bind which happens in Java version greater than 1.8 if you don’t provide the JAXB API and implementation classes.

Found the article helpful? if so please follow us on Socials