How to Resolve Java.Lang.Noclassdeffounderror: Javax/Xml/Bind/Jaxbexception

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

The JAXB APIs are considered to be Java EE APIs and therefore are no longer contained on the default classpath in Java SE 9. In Java 11, they are completely removed from the JDK.

Java 9 introduces the concepts of modules, and by default, the java.se aggregate module is available on the classpath (or rather, module-path). As the name implies, the java.se aggregate module does not include the Java EE APIs that have been traditionally bundled with Java 6/7/8.

Fortunately, these Java EE APIs that were provided in JDK 6/7/8 are still in the JDK, but they just aren't on the classpath by default. The extra Java EE APIs are provided in the following modules:

java.activation
java.corba
java.transaction
java.xml.bind << This one contains the JAXB APIs
java.xml.ws
java.xml.ws.annotation

Quick and dirty solution: (JDK 9/10 only)

To make the JAXB APIs available at runtime, specify the following command-line option:

--add-modules java.xml.bind

But I still need this to work with Java 8!!!

If you try specifying --add-modules with an older JDK, it will blow up because it's an unrecognized option. I suggest one of two options:

  1. You can set any Java 9+ only options using the JDK_JAVA_OPTIONS environment variable. This environment variable is automatically read by the java launcher for Java 9+.
  2. You can add the -XX:+IgnoreUnrecognizedVMOptions to make the JVM silently ignore unrecognized options, instead of blowing up. But beware! Any other command-line arguments you use will no longer be validated for you by the JVM. This option works with Oracle/OpenJDK as well as IBM JDK (as of JDK 8sr4).

Alternate quick solution: (JDK 9/10 only)

Note that you can make all of the above Java EE modules available at run time by specifying the --add-modules java.se.ee option. The java.se.ee module is an aggregate module that includes java.se.ee as well as the above Java EE API modules. Note, this doesn't work on Java 11 because java.se.ee was removed in Java 11.



Proper long-term solution: (JDK 9 and beyond)

The Java EE API modules listed above are all marked @Deprecated(forRemoval=true) because they are scheduled for removal in Java 11. So the --add-module approach will no longer work in Java 11 out-of-the-box.

What you will need to do in Java 11 and forward is include your own copy of the Java EE APIs on the classpath or module path. For example, you can add the JAX-B APIs as a Maven dependency like this:

<!-- 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>

See the JAXB Reference Implementation page for more details on JAXB.

For full details on Java modularity, see JEP 261: Module System

As of July 2022, the latest version of the bind-api and jaxb-runtime is 4.0.0. So you can also use

    <version>4.0.0</version>

...within those dependency clauses. But if you do so, the package names have changed from javax.xml.bind... to jakarta.xml.bind.... You will need to modify your source code to use these later versions of the JARs.

For Gradle or Android Studio developer: (JDK 9 and beyond)

Add the following dependencies to your build.gradle file:

dependencies {
// JAX-B dependencies for JDK 9+
implementation "jakarta.xml.bind:jakarta.xml.bind-api:2.3.2"
implementation "org.glassfish.jaxb:jaxb-runtime:2.3.2"
}

java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException after updating to Android Studio 4.2

Android Studio 4.2.0 and more now comes with Java 11.0.8 shipped and will use it by default which cases this error in deprecated libraries.

The solution is simple, you need to change it to java (JDK) 1.8 in the menu File > Project Structure > JDK Location

image

Notices that Android Studio lower than 4.2.0 comes with java 1.8 already

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.

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

Regarding jaxb missing add these

<!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-core -->
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0</version>
</dependency>

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

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

<!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-impl -->
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>

If you are planning to use Hibernate you will be needing javax packages as well they are from this dependency

<!-- https://mvnrepository.com/artifact/com.sun.activation/javax.activation -->
<dependency>
<groupId>com.sun.activation</groupId>
<artifactId>javax.activation</artifactId>
<version>1.2.0</version>
</dependency>

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

Deleting the other jdk versions from Library/Java/JavaVirtualMachines solved the problem for me



Related Topics



Leave a reply



Submit