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

I have code that uses the JAXB API classes that were provided as part of the JDK in Java 6/7/8. When I run the same code with Java 9, at runtime I get errors indicating that the JAXB classes cannot be found.

JAXB classes were provided as part of the JDK with Java 6, so why can't Java 9 find these classes anymore?

+686
java java-9 jaxb java-10 java-11
Apr 23 '17 at 17:40
source share
24 answers

JAXB APIs are considered Java EE APIs and therefore are no longer contained in the default class path in Java SE 9. In Java 11, they are completely removed from the JDK.

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

Fortunately, these Java EE APIs that were provided in the JDK 6/7/8 are still in the JDK, but they simply are not in the way of the default classes. Additional 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 JAXB APIs available at run time, specify the following command line parameter:
--add-modules java.xml.bind

But I still need this to work with Java 8 !!!
If you try to specify --add-modules with the old JDK, it will explode because it is an unrecognized option. I offer one of two options:

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



Alternative quick fix: (JDK 9/10 only)
Please note that you can make all of the above Java EE modules available at run time by specifying the --add-modules java.se.ee The java.se.ee module is an aggregate module that includes java.se.ee and also the above Java EE API modules.




The right long-term solution: (JDK 9 and above)

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

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

 <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.2.11</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-core</artifactId> <version>2.2.11</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>2.2.11</version> </dependency> <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> <version>1.1.1</version> </dependency> 

For complete information on Java modularity, see JEP 261: Modular System

For Gradle or Android Studio developers: (JDK 9 and above)

Add the following dependencies to your build.gradle file:

 dependencies { // JAX-B dependencies for JDK 9+ implementation "javax.xml.bind:jaxb-api:2.2.11" implementation "com.sun.xml.bind:jaxb-core:2.2.11" implementation "com.sun.xml.bind:jaxb-impl:2.2.11" implementation "javax.activation:activation:1.1.1" } 
+1026
Apr 23 '17 at 17:40
source share

In my case (spring boot sapphire) I just add the following to pom.xml.

 <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.0</version> </dependency> 
+212
Nov 21 '17 at 12:07 on
source share

None of these solutions worked for me in the recent JDK 9.0.1.

I found that this list of dependencies is enough to work correctly, so you do not need to explicitly specify --add-module (although it is listed in these pom's dependencies). You only need to specify this list of dependencies:

 <dependencies> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>org.glassfish.jaxb</groupId> <artifactId>jaxb-runtime</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> <version>1.1.1</version> </dependency> </dependencies> 
+61
Jan 07 '18 at 12:06
source share

This worked for me:

 <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>org.eclipse.persistence</groupId> <artifactId>eclipselink</artifactId> <version>2.7.0</version> </dependency> 



Refresh

As @Jasper suggested, to avoid dependency on the entire EclipseLink library, you can also just depend on EclipseLink MOXy:

specialist

 <dependency> <groupId>org.eclipse.persistence</groupId> <artifactId>org.eclipse.persistence.moxy</artifactId> <version>2.7.3</version> </dependency> 

Gradle

 compile group: 'org.eclipse.persistence', name: 'org.eclipse.persistence.moxy', version: '2.7.3' 



As dependencies for my Java 8 application, which creates a * .jar that can run both JRE 8 and JRE 9 without additional arguments.

In addition, this should be done somewhere before using the JAXB API:

 System.setProperty("javax.xml.bind.JAXBContextFactory", "org.eclipse.persistence.jaxb.JAXBContextFactory"); 

Still works great as a workaround. Doesn't seem like a perfect solution though ...

+39
Sep 27 '17 at 18:40
source share

this is because the java version, if you are using JDK 9 or later, just add this to your POM

 <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.0</version> </dependency> 
+32
Oct 29 '18 at 6:34
source share

At compile time as well as run time, add the --add-modules java.xml.bind

 javac --add-modules java.xml.bind <java file name> java --add-modules java.xml.bind <class file> 

A good introduction to JDK 9 modules can also be found at: https://www.youtube.com/watch?v=KZfbRuvv5qc

+17
Apr 25 '17 at 4:35 on
source share

pure solution for all JDK> = 9

You need to add two dependencies to your assembly.

  • jaxb-api
  • jaxb implementation

As an implementation, I decided to use the reference implementation using glass fish to get rid of the old com.sun classes / libraries. So I added maven to my assembly

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

Please note that from version 2.3.1 you no longer need to add javax.activation. (see https://github.com/eclipse-ee4j/jaxb-ri/issues/1222 )

+17
Oct 18 '18 at 10:37
source share
+15
Jan 23 '18 at 14:53
source share

You can use the --add-modules=java.xml.bind JVM --add-modules=java.xml.bind to add the xml binding module to the JVM runtime.

For example: java --add-modules=java.xml.bind XmlTestClass

+11
Oct 10 '17 at 19:27
source share

It worked for me. Adding just jaxb-api was not enough.

  <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>${jaxb-api.version}</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>${jaxb-api.version}</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-core</artifactId> <version>${jaxb-api.version}</version> </dependency> 
+10
Dec 04 '17 at 18:24
source share

April 2019 update

Changelong for JAXB releases is located at https://javaee.imtqy.com/jaxb-v2/doc/user-guide/ch02.html.

excerpts:

  4.1. Changes between 2.3.0.1 and 2.4.0 JAXB RI is now JPMS modularized: All modules have native module descriptor. Removed jaxb-core module, which caused split package issue on JPMS. RI binary bundle now has single jar per dependency instead of shaded fat jars. Removed runtime class weaving optimization. 4.2. Changes between 2.3.0 and 2.3.0.1 Removed legacy technology dependencies: com.sun.xml.bind:jaxb1-impl net.java.dev.msv:msv-core net.java.dev.msv:xsdlib com.sun.xml.bind.jaxb:isorelax 4.3. Changes between 2.2.11 and 2.3.0 Adopt Java SE 9: JAXB api can now be loaded as a module. JAXB RI is able to run on Java SE 9 from the classpath. Addes support for java.util.ServiceLoader mechanism. Security fixes 

Official link at https://github.com/eclipse-ee4j/jaxb-ri#maven-artifacts

Maven coordinates for JAXB artifacts

jakarta.xml.bind: jakarta.xml.bind-api: API classes for JAXB. Required to compile with JAXB.

org.glassfish.jaxb: jaxb-runtime: JAXB implementation, a runtime used to serialize and deserialize java objects to / from xml.

JAXB Bundles:

com.sun.xml.bind: jaxb-impl: JAXB is a fat run-time jar.

Unlike the artifacts of org.glassfish.jaxb, these jar files contain all the dependency classes. These artifacts do not contain JPMS module descriptors. Maven projects use org.glassfish.jaxb artifacts instead.

org.glassfish.jaxb: jaxb-runtime: jar: 2.3.2 loads:

 [INFO] +- org.glassfish.jaxb:jaxb-runtime:jar:2.3.2:compile [INFO] | +- jakarta.xml.bind:jakarta.xml.bind-api:jar:2.3.2:compile [INFO] | +- org.glassfish.jaxb:txw2:jar:2.3.2:compile [INFO] | +- com.sun.istack:istack-commons-runtime:jar:3.0.8:compile [INFO] | +- org.jvnet.staxex:stax-ex:jar:1.8.1:compile [INFO] | +- com.sun.xml.fastinfoset:FastInfoset:jar:1.2.16:compile [INFO] | \- jakarta.activation:jakarta.activation-api:jar:1.2.1:compile 

Original answer

After What artifacts should I use for JAXB RI in my Maven project? in Maven you can use a profile like:

 <profile> <id>java-9</id> <activation> <jdk>9</jdk> </activation> <dependencies> <dependency> <groupId>org.glassfish.jaxb</groupId> <artifactId>jaxb-runtime</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> <version>1.1.1</version> </dependency> </dependencies> </profile> 

The dependency tree shows:

 [INFO] +- org.glassfish.jaxb:jaxb-runtime:jar:2.3.0:compile [INFO] | +- org.glassfish.jaxb:jaxb-core:jar:2.3.0:compile [INFO] | | +- javax.xml.bind:jaxb-api:jar:2.3.0:compile [INFO] | | +- org.glassfish.jaxb:txw2:jar:2.3.0:compile [INFO] | | \- com.sun.istack:istack-commons-runtime:jar:3.0.5:compile [INFO] | +- org.jvnet.staxex:stax-ex:jar:1.7.8:compile [INFO] | \- com.sun.xml.fastinfoset:FastInfoset:jar:1.2.13:compile [INFO] \- javax.activation:activation:jar:1.1.1:compile 

To use this in Eclipse, say, Oxygen.3a Release (4.7.3a) or later, Ctrl-Alt-P or right-click on the Maven project, then select a profile.

+10
May 6 '18 at 3:19
source share

Go to your Build.gradle and add the dependencies below for Java 9 or Java 10.

 sourceCompatibility = 10 // You can also decrease your souce compatibility to 1.8 //java 9+ does not have Jax B Dependents compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.0' compile group: 'com.sun.xml.bind', name: 'jaxb-core', version: '2.3.0' compile group: 'com.sun.xml.bind', name: 'jaxb-impl', version: '2.3.0' compile group: 'javax.activation', name: 'activation', version: '1.1.1' 
+9
Jul 08 '18 at 18:51
source share

For Java Web Start Execution, we can use Andy Gibert's suggestion as follows:

 <j2se version="1.6+" java-vm-args="-XX:+IgnoreUnrecognizedVMOptions --add-modules=java.se.ee"/> 

Note the extra "=" in -add-modules. See this OpenJDK ticket or last note in the Understanding Time Access Alerts Java Platform Standard Edition Oracle JDK 9 Migration Guide .

+8
Sep 29 '17 at 8:34 on
source share

add javax.xml.bind dependency to pom.xml

  <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.0</version> </dependency> 
+8
Oct 17 '18 at 19:02
source share

I followed this url and the settings below really helped me. I am using Java 10 with STS IDE on Macbook Pro. It works great.

  <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>org.glassfish.jaxb</groupId> <artifactId>jaxb-runtime</artifactId> <version>2.3.0</version> <scope>runtime</scope> </dependency> <dependency> <groupId>javax.activation</groupId> <artifactId>javax.activation-api</artifactId> <version>1.2.0</version> </dependency> 
+6
Sep 15 '18 at 16:12
source share

I ran into the same problem using Spring Boot 2.0.5.RELEASE in Java 11.

Adding javax.xml.bind:jaxb-api:2.3.0 not javax.xml.bind:jaxb-api:2.3.0 problem. I also had to upgrade Spring Boot to the latest 2.1.0.M2 Milestone 2.1.0.M2 , so I assume this will be fixed in the next official version.

+6
Sep 29 '18 at 4:25
source share

Not an answer, but an addendum: I got it because groovysh (Groovy 2.4.13) was running, if JAVA_HOME indicates a Java 9 installation (more precisely, java version "9.0.1" ) fails:

 java.lang.reflect.InvocationTargetException at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:564) at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:107) at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:129) Caused by: java.lang.NoClassDefFoundError: Unable to load class groovy.xml.jaxb.JaxbGroovyMethods due to missing dependency javax/xml/bind/JAXBContext at org.codehaus.groovy.vmplugin.v5.Java5.configureClassNode(Java5.java:400) at org.codehaus.groovy.ast.ClassNode.lazyClassInit(ClassNode.java:277) at org.codehaus.groovy.ast.ClassNode.getMethods(ClassNode.java:397) ... .. . .. ... at org.codehaus.groovy.tools.shell.Groovysh.<init>(Groovysh.groovy:135) at org.codehaus.groovy.vmplugin.v7.IndyInterface.selectMethod(IndyInterface.java:232) at org.codehaus.groovy.tools.shell.Main.<init>(Main.groovy:66) at org.codehaus.groovy.vmplugin.v7.IndyInterface.selectMethod(IndyInterface.java:232) at org.codehaus.groovy.tools.shell.Main.main(Main.groovy:163) ... 6 more 

The solution was as follows:

  • Go to the JAXB project at imtqy.com ("JAXB is licensed under a dual license - CDDL 1.1 and GPL 2.0 with class exception")

  • Download jaxb-ri-2.3.0.zip

  • Unzip wherever you put the java infrastructure files (in my case, /usr/local/java/jaxb-ri/ ). Another solution may exist (maybe via SDKMAN, I don't know)

  • Make sure the banks in the lib subdirectory are in CLASSPATH . I do this through a script running at bash startup called /etc/profile.d/java.sh , where I added (among many other lines) the following loop:

Packed in function ...

 function extend_qzminynshg { local BASE="/usr/local/java" for LIB in jaxb-api.jar jaxb-core.jar jaxb-impl.jar jaxb-jxc.jar jaxb-xjc.jar; do local FQLIB="$BASE/jaxb-ri/lib/$LIB" if [[ -f $FQLIB ]]; then export CLASSPATH=$FQLIB:$CLASSPATH fi done } extend_qzminynshg; unset extend_qzminynshg 

And it works!

+3
Jan 04 '18 at 20:46
source share

Since JavaEE is now regulated by https://jakarta.ee/ , the new Maven coordinates are from 2.3.2:

https://github.com/eclipse-ee4j/jaxb-ri#maven-artifacts

The first jaxb.version released is 2.3.2.

 <properties> <jaxb.version>2.3.2</jaxb.version> </properties> <dependency> <groupId>jakarta.xml.bind</groupId> <artifactId>jakarta.xml.bind-api</artifactId> <version>${jaxb.version}</version> </dependency> <dependency> <groupId>org.glassfish.jaxb</groupId> <artifactId>jaxb-runtime</artifactId> <version>${jaxb.version}</version> </dependency> 
+3
Apr 11 '19 at 6:55
source share

OK, I had the same problem, but I used Java 8 and kept getting this error, I tried most of the solutions. but it turns out that my maven was still pointing to java 9, although I installed the global version of Java on 8 as soon as I fixed that it worked.

For those who may have such problems, check out How to Fix Maven to Use Java by Default

+2
Feb 22 '18 at 11:39
source share

Old answer "The problem is solved by switching to amazoncorretto." Answer in the news: I used the latest version of corretto, but similar to jdk 1.8. so anyway we need to add dependencies manually

+2
Apr 22 '19 at 7:48
source share

The best solution in the long run is to add a Maven dependency.

If necessary, declare a new profile different from the standard one for Java 9, and install the dependency there. Thus, you can move smoothly.

+1
Dec 16 '17 at 5:42 on
source share

I know I'm late for the party, but my mistake ended up requiring a different solution ... also very simple

I initially depolved on Tomcat 9 and realized that I needed 7 ... I forgot to display my class path back to version 7 in build.xml

Hope this fixes the mistake of someone else in the future who will be able to skip this simple problem like me!

0
Oct 26 '18 at 19:37
source share

This worked for me, I have a spring boot project that compiles in Java 8, but I don’t know why one day my maven started compiling with Java 11, in Ubuntu I used it to fix it:

 sudo update-java-alternatives -l 

This showed me the available JDK on my computer:

 java-1.11.0-openjdk-amd64 1111 /usr/lib/jvm/java-1.11.0-openjdk-amd64 

java-1.8.0-openjdk-amd64 1081/usr/lib/jvm/java-1.8.0-openjdk-amd64

So, I finally ran this command to select what I wanted:

 sudo update-java-alternatives -s java-1.8.0-openjdk-amd64 

And what is it, for a better look at How to use team update alternatives

0
Jun 04 '19 at 11:27
source share

The dependency versions that I needed to use when compiling for the purpose of Java 8. The application was tested in the Java 8, 11, and 12 JREs.

  <!-- replace dependencies that have been removed from JRE starting with Java v11 --> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.2.8</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-core</artifactId> <version>2.2.8-b01</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>2.2.8-b01</version> </dependency> <!-- end replace dependencies that have been removed from JRE starting with Java v11 --> 
0
Jun 08 '19 at 20:01
source share



All Articles