I have some code that uses JAXB API classes which have been provided as a 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 JAXB classes can not be found.
The JAXB classes have been provided as a part of the JDK since Java 6, so why can Java 9 no longer find these classes?
-
2The additional part in this answer relates to the migration of these API's.Naman– Naman2017年10月21日 07:05:07 +00:00Commented Oct 21, 2017 at 7:05
-
11building with Java 8 will get your code to compile yes, but when you try to run that compiled code on Java 9+ it will fail because JAX-B is not present.Andy Guibert– Andy Guibert2018年10月24日 12:07:46 +00:00Commented Oct 24, 2018 at 12:07
-
2For Java 11, this article's solution is up to date: crunchify.com/java-11-and-javax-xml-bind-jaxbcontextEric– Eric2019年01月11日 09:33:19 +00:00Commented Jan 11, 2019 at 9:33
-
see wiki.bitplan.com/index.php/Java8Wolfgang Fahl– Wolfgang Fahl2019年10月10日 13:41:21 +00:00Commented Oct 10, 2019 at 13:41
-
See the way I fixed it her :- AnswerRuchira– Ruchira2021年04月24日 02:17:27 +00:00Commented Apr 24, 2021 at 2:17
46 Answers 46
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:
- You can set any Java 9+ only options using the
JDK_JAVA_OPTIONS
environment variable. This environment variable is automatically read by thejava
launcher for Java 9+. - 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 Eclipse Implementation of JAXBTM and the GitHub site for more details on that implementation. Another implementation is EclipseLink MOXy.
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"
}
-
10So if the Java EE API modules are marked deprecated does that mean it is a possibility that in Java 10 JAXB will no longer be available at runtime in Java 10? That seems like a step backwards. We will have to go back to the pre-6 practice of including JAXB as a dependency.Michael– Michael2017年09月22日 17:35:10 +00:00Commented Sep 22, 2017 at 17:35
-
4Using --add-modules java.se.ee or --add-modules ALL-SYSTEM as a workaround is not recommended according to migration guide here docs.oracle.com/javase/9/migrate under section Modules Shared with Java EE Not Resolved by Default --> point 1justMe– justMe2017年10月20日 14:32:38 +00:00Commented Oct 20, 2017 at 14:32
-
7With Java 10 officially released, we can confirm that the add-modules method will still work. The
javax.xml.bind
and other JavaEE classes are scheduled for removal in Java 11, per JEP-320.Joep Weijers– Joep Weijers2018年03月21日 11:02:01 +00:00Commented Mar 21, 2018 at 11:02 -
16And now Java 11 is released and the
java.se.ee
module has been removed, so the--add-modules
solution does not work anymore. Use the recommended solution instead: add JAXB as a separate dependency.Jesper– Jesper2018年09月30日 12:30:17 +00:00Commented Sep 30, 2018 at 12:30 -
63i've added these dependencies and it is still giving me the same error. any ideas why?João Vieira– João Vieira2019年07月08日 17:16:23 +00:00Commented Jul 8, 2019 at 17:16
In my case (spring boot fat jar) I just add the following to pom.xml.
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
-
14Just for reference github.com/spring-projects/spring-boot/wiki/…Tuno– Tuno2018年03月08日 10:08:15 +00:00Commented Mar 8, 2018 at 10:08
-
10Adding gradle dependency like this
testCompile('javax.xml.bind:jaxb-api')
worked for me.pamcevoy– pamcevoy2018年05月17日 15:07:59 +00:00Commented May 17, 2018 at 15:07 -
6Like @pamcevoy mentioned, no need to specify the version of jaxb-api when using Spring Boot. Boot manages the version automatically.Marcel Overdijk– Marcel Overdijk2018年07月12日 13:32:33 +00:00Commented Jul 12, 2018 at 13:32
-
4I suggest to use
<scope>runtime</scope>
for such caseVladS– VladS2018年09月18日 22:43:06 +00:00Commented Sep 18, 2018 at 22:43 -
7@Tuno's link didn't work for me, fixed link is: github.com/spring-projects/spring-boot/wiki/…Cisco– Cisco2018年12月12日 01:01:20 +00:00Commented Dec 12, 2018 at 1:01
Clean solution for all JDKs >= 9
You need to add two dependencies to your build
- the jaxb-api
- a jaxb implementation
As an implementation I chose to use the reference implementation by glassfish to get rid of old com.sun classes / libraries. So as a result I added in my maven build
<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>
Note that from version 2.3.1 you don't need to add the javax.activation any longer. (see https://github.com/eclipse-ee4j/jaxb-ri/issues/1222)
-
Is the javax.xml.bind module really required? My code in JDK 11 works without it.k.liakos– k.liakos2018年11月01日 16:11:03 +00:00Commented Nov 1, 2018 at 16:11
-
@k.liakos I am not sure. The jaxb-runtime jar and the api-jar do not share the same classes/packages. I guess it depends on your code. If your code does not use the classes from the package 'javax.xml.bind' then you probably don't need it. The topic of this thread is that 'javax/xml/bind/JAXBException' cannot be found; this class is only in the jaxb-api.Sebastian Thees– Sebastian Thees2018年11月02日 09:30:16 +00:00Commented Nov 2, 2018 at 9:30
-
2Works perfectly with multi-module project in java 12.Heril Muratovic– Heril Muratovic2019年06月24日 12:23:29 +00:00Commented Jun 24, 2019 at 12:23
-
5
-
where is pom.xml file in project which is writable , in my project all pom.xml file only readbale, Is I need to create another one, if yes where i create this. @SebastianTheesSurajkaran Meghwanshi– Surajkaran Meghwanshi2023年01月11日 06:38:00 +00:00Commented Jan 11, 2023 at 6:38
None of these solutions worked fine for me in the recent JDK 9.0.1.
I found that this list of dependencies is enough for a proper functioning, so you don't need to explicitly specify --add-module
(though it is specified within these dependencies's pom's). The only you need is 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>
-
2For JDK 8, remove the jaxb-core and jaxb-impl from above.foo– foo2018年01月19日 04:48:50 +00:00Commented Jan 19, 2018 at 4:48
-
4@Anil this is a
pom.xml
file of the Maven configuration. If you don't know what is that, then it is better to start from begginingAndremoniy– Andremoniy2018年02月13日 13:16:30 +00:00Commented Feb 13, 2018 at 13:16 -
8An illegal reflective access operation has occurred WARNING: Illegal reflective access by com.sun.xml.bind.v2.runtime.reflect.opt.Injector (file:/C:/Users/eis/.m2/repository/com/sun/xml/bind/jaxb-impl/2.3.0/jaxb-impl-2.3.0.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int) WARNING: Please consider reporting this to the maintainers of com.sun.xml.bind.v2.runtime.reflect.opt.Injector WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a future releaseStefan– Stefan2018年02月19日 10:02:52 +00:00Commented Feb 19, 2018 at 10:02
-
1This worked for me on JDK 9.0.4 (I was calling JAXB related code through a Maven plugin with Maven 3.5.3). Although I would use
<dependency> <groupId>javax.activation</groupId> <artifactId>javax.activation-api</artifactId> <version>1.2.0</version> </dependency>
as last dependency.scrutari– scrutari2018年04月20日 22:58:33 +00:00Commented Apr 20, 2018 at 22:58 -
2Awesome. I had a situation where - for some reason - a spring boot app would run in intellij CE but not eclipse on mac, and in eclipse but not intellij CE on win10. Being able to work in one IDE on two platforms is an advantage.kometen– kometen2018年05月25日 13:22:17 +00:00Commented May 25, 2018 at 13:22
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>
Update
As @Jasper suggested, in order to avoid depending on the entire EclipseLink library, you can also just depend on EclipseLink MOXy:
Maven
<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 app, which produces a *.jar which can be run by both JRE 8 or JRE 9 with no additional arguments.
In addition, this needs to be executed somewhere before JAXB API will be used:
System.setProperty("javax.xml.bind.JAXBContextFactory", "org.eclipse.persistence.jaxb.JAXBContextFactory");
Works great so far, as a workaround. Doesn't look like a perfect solution though...
-
6adding
org.eclipse.persistence:eclipselink
just to get JAXB APIs is a very heavy-weight dependency, unless you are already using eclipselink?Andy Guibert– Andy Guibert2017年09月27日 18:59:51 +00:00Commented Sep 27, 2017 at 18:59 -
4Yes it is heavy (~9mb) and yes I've been using that already. I've mentioned that this is simply an alternative workaround for those who, maybe temporary, will have to use both 8 and 9 JREs for the same jar/war without providing command-line arguments.Mikhail Kholodkov– Mikhail Kholodkov2017年09月27日 19:33:39 +00:00Commented Sep 27, 2017 at 19:33
-
2for the sake of interop between JDK 8 & 9, I would recommend using the
-XX:+IgnoreUnrecognizedVMOptions
command line option (updated my answer with details)Andy Guibert– Andy Guibert2017年09月27日 19:55:30 +00:00Commented Sep 27, 2017 at 19:55 -
System.setProperty("javax.xml.bind.JAXBContextFactory", "org.eclipse.persistence.jaxb.JAXBContextFactory"); does not work for meDavid Brossard– David Brossard2018年02月16日 18:00:03 +00:00Commented Feb 16, 2018 at 18:00
-
1To avoid depending on the entire EclipseLink library, you can also just depend on EclipseLink MOXy: groupId
org.eclipse.persistence
, artifactIdorg.eclipse.persistence.moxy
.Jesper– Jesper2018年09月30日 13:14:42 +00:00Commented Sep 30, 2018 at 13:14
it ́s because java version if you are using jdk 9 or a later version just add this to your pom
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
-
2I run into this all the time with the Spring Boot guides... Thanks a ton.masterxilo– masterxilo2018年11月07日 15:39:13 +00:00Commented Nov 7, 2018 at 15:39
-
2@Cesar Rodriguez T, I tried this with a swagger example and compiling worked but running gave errors. I used the selected answer which included more dependencies and that worked.PatS– PatS2018年11月13日 13:24:45 +00:00Commented Nov 13, 2018 at 13:24
-
On pom.xml file of your projectCesar Rodriguez– Cesar Rodriguez2019年11月13日 04:34:16 +00:00Commented Nov 13, 2019 at 4:34
-
In my project two pom.xml i found but they not writable , its only in readable format what i need to do? @CesarRodriguezSurajkaran Meghwanshi– Surajkaran Meghwanshi2023年01月11日 06:41:43 +00:00Commented Jan 11, 2023 at 6:41
To solve this, I have imported some JAR files in my project:
- javax.activation-1.2.0.jar
- jaxb-api-2.3.0.jar
http://search.maven.org/remotecontent?filepath=javax/xml/bind/jaxb-api/2.3.0/jaxb-api-2.3.0.jar
- jaxb-core-2.3.0.jar
http://search.maven.org/remotecontent?filepath=com/sun/xml/bind/jaxb-core/2.3.0/jaxb-core-2.3.0.jar
- jaxb-impl-2.3.0.jar
http://search.maven.org/remotecontent?filepath=com/sun/xml/bind/jaxb-impl/2.3.0/jaxb-impl-2.3.0.jar
- Download above files and copy them into libs folder in the project
- Add the imported JAR files in Java Build Path
-
4Note that the
com.sun.xml.bind
artifacts are old and provided only for backward compatibility. You should use the equivalentorg.glassfish.jaxb
artifacts instead, as mentioned in some of the other answers.Jesper– Jesper2018年09月30日 12:59:47 +00:00Commented Sep 30, 2018 at 12:59 -
This didn't work for me. It threw an error, and said that it couldn't find a particular class.RamenChef– RamenChef2018年10月15日 02:15:50 +00:00Commented Oct 15, 2018 at 2:15
-
1Worked for me when I put them in tomcat9/lib folder under Mint 19.2 (Ubuntu 18.04 base), when deploying a Grails 3.4.10 application.Mohamad Fakih– Mohamad Fakih2019年12月05日 20:53:12 +00:00Commented Dec 5, 2019 at 20:53
-
Didn't work, instead raised more errors requiring several other jar inclusions. Mine was for jdk8vibhor vaish– vibhor vaish2021年02月25日 07:22:21 +00:00Commented Feb 25, 2021 at 7:22
At the time of compilation as well as run time, add the switch --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 of the JDK 9
modules can also be found at :
https://www.youtube.com/watch?v=KZfbRuvv5qc
I encountered this issue when working on a Java Project in Debian 10.
Each time I start the appliction it throws the error in the log file:
java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
Here's how I solved it:
The issue is often caused when JAXB library (Java Architecture for XML Binding) is missing in the classpath. JAXB is included in Java SE 10 or older, but it is removed from Java SE from Java 11 or newer –moved to Java EE under Jakarta EE project.
So, I checked my Java version using:
java --version
And it gave me this output
openjdk 11.0.8 2020年07月14日
OpenJDK Runtime Environment (build 11.0.8+10-post-Debian-1deb10u1)
OpenJDK 64-Bit Server VM (build 11.0.8+10-post-Debian-1deb10u1, mixed mode, sharing)
So I was encountering the JAXBException error because I was using Java 11, which does not have the JAXB library (Java Architecture for XML Binding) is missing in the classpath. JAXB is included in it.
To fix the issue I had to add the JAXB API library to the lib (/opt/tomcat/lib
) directory of my tomcat installation:
sudo wget https://repo1.maven.org/maven2/javax/xml/bind/jaxb-api/2.4.0-b180830.0359/jaxb-api-2.4.0-b180830.0359.jar
Then I renamed it from jaxb-api-2.4.0-b180830.0359.jar
to jaxb-api.jar
:
sudo mv jaxb-api-2.4.0-b180830.0359.jar jaxb-api.jar
Note: Ensure that you change the permission allow tomcat access the file and also change the ownership to tomcat
:
sudo chown -R tomcat:tomcat /opt/tomcat
sudo chmod -R 777 /opt/tomcat/
And then I restarted the tomcat server:
sudo systemctl restart tomcat
Resources: [Solved] java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
That's all.
-
in case of a spring boot app ?Tiago Medici– Tiago Medici2024年07月17日 09:11:53 +00:00Commented Jul 17, 2024 at 9:11
I also stumpled accross the ClassNotFoundException:javax.xml.bind.DatatypeConverter using Java 11 and
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
I tried all this stuff around adding javax.xml.bind:jaxb-api or spring boot jakarta.xml.bind-api .. I found a hint for fixes in jjwt version 0.10.0 .. but most importantly, the jjwt package is now split !
Thus, check this reference: https://github.com/jwtk/jjwt/issues/510
Simply, if you use
Java11 and jjwt 0.9.x and you face the ClassNotFoundException:javax.xml.bind.DatatypeConverter issue,
go for
jjwt version 0.11.x, but use the splitted packages: https://github.com/jwtk/jjwt#install
You maven wont find a higher version for jjwt dependency, since they split the packages.
Cheers.
-
Using jjwt from io.jsonwebtoken version 0.9.1 with java 16 also caused the same error. The root cause is the same of java 11. Using jjwt-api and jjwt-impl (from io.jsonwebtoken) with version 0.10.7 solved the issue here.lubrum– lubrum2021年07月21日 18:03:01 +00:00Commented Jul 21, 2021 at 18:03
Update April 2019
Changelong for JAXB releases is at https://javaee.github.io/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
Authoritative link is 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 against JAXB.
org.glassfish.jaxb:jaxb-runtime: Implementation of JAXB, runtime used for serialization and deserialization java objects to/from xml.
JAXB fat-jar bundles:
com.sun.xml.bind:jaxb-impl: JAXB runtime fat jar.
In contrast to org.glassfish.jaxb artifacts, these jars have all dependency classes included inside. These artifacts does not contain JPMS module descriptors. In Maven projects org.glassfish.jaxb artifacts are supposed to be used instead.
org.glassfish.jaxb:jaxb-runtime:jar:2.3.2 pulls in:
[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
Following Which 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>
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 project, Maven, then select the profile.
-
Thanks for showing that a dependency for
javax.xml.bind
>jaxb-api
that I have seen elsewhere is actually redundant. The glassfish dependency pulls it is. I just tried that, and it does indeed work.Basil Bourque– Basil Bourque2018年07月23日 05:42:19 +00:00Commented Jul 23, 2018 at 5:42
You need to add JAX-B dependencies when using JDK 9+. For Android Studio user, you'll need to add this to your build.gradle
's dependencies {}
block:
// Add missing dependencies for JDK 9+
if (JavaVersion.current().ordinal() >= JavaVersion.VERSION_1_9.ordinal()) {
// If you're using @AutoValue or any libs that requires javax.annotation (like Dagger)
compileOnly 'com.github.pengrad:jdk9-deps:1.0'
compileOnly 'javax.annotation:javax.annotation-api:1.3.2'
// If you're using Kotlin
kapt "com.sun.xml.bind:jaxb-core:2.3.0.1"
kapt "javax.xml.bind:jaxb-api:2.3.1"
kapt "com.sun.xml.bind:jaxb-impl:2.3.2"
// If you're using Java
annotationProcessor "com.sun.xml.bind:jaxb-core:2.3.0.1"
annotationProcessor "javax.xml.bind:jaxb-api:2.3.1"
testAnnotationProcessor "com.sun.xml.bind:jaxb-core:2.3.0.1"
testAnnotationProcessor "javax.xml.bind:jaxb-api:2.3.1"
}
-
I have modified your answer to work with Unit tests as well.Malachiasz– Malachiasz2020年03月04日 10:22:10 +00:00Commented Mar 4, 2020 at 10:22
-
3This is the only solution that worked for me!Dhruvam Sharma– Dhruvam Sharma2021年05月05日 07:48:27 +00:00Commented May 5, 2021 at 7:48
-
This is the best solution!!!Alex Rivas– Alex Rivas2021年12月05日 19:34:58 +00:00Commented Dec 5, 2021 at 19:34
This worked for me. Adding only jaxb-api wasn't 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>
-
And what was jaxb-api.version set to?MiguelMunoz– MiguelMunoz2017年12月11日 03:42:00 +00:00Commented Dec 11, 2017 at 3:42
-
@MiguelMunoz I used 2.2.7Mr Jedi– Mr Jedi2017年12月11日 11:41:55 +00:00Commented Dec 11, 2017 at 11:41
-
6Note that the
com.sun.xml.bind
artifacts are old and provided only for backward compatibility. You should use the equivalentorg.glassfish.jaxb
artifacts instead, as mentioned in some of the other answers.Jesper– Jesper2018年09月30日 12:58:28 +00:00Commented Sep 30, 2018 at 12:58
The root cause of this issue is that Gradle Daemon using JDK11, either you set your JAVA_HOME to JDK11 or your running your Gradle Task in the shared daemon which running with JDK11.
For Android:
Check your Project Structure settings, you can change the JDK to JDK8 from there.
You can also set a JAVA_HOME and points to java8 home.
Go to Your Build.gradle and add below dependencies for both 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'
You can use --add-modules=java.xml.bind
JVM option to add xml bind module to JVM run-time environment.
Eg: java --add-modules=java.xml.bind XmlTestClass
Adding the below dependency worked for me.
<!-- 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>
-
3Make a note that javax libraries are deprecated since jdk 11. Just replace javax dependencies with Jakarta libraries dependencies referring below link. wiki.eclipse.org/Jakarta_EE_Maven_CoordinatesAbhishek Singh– Abhishek Singh2021年12月15日 04:58:20 +00:00Commented Dec 15, 2021 at 4:58
-
There is not javx lib in the above two dependencies.Pijush– Pijush2025年04月02日 09:57:02 +00:00Commented Apr 2 at 9:57
For Java Web Start Execution we can use Andy Guibert's suggestion like this:
<j2se version="1.6+"
java-vm-args="-XX:+IgnoreUnrecognizedVMOptions --add-modules=java.se.ee"/>
Note the extra "=" in the --add-modules. See this OpenJDK Ticket or the last note in "Understanding Runtime Access Warnings" of the Java Platform, Standard Edition Oracle JDK 9 Migration Guide.
add javax.xml.bind dependency in pom.xml
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
This solved my problems with dependencies running Apache Camel 2.24.1 on Java 12:
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0.1</version>
</dependency>
-
in my case i need to add tomcat dependency in pom fileGvSharma– GvSharma2019年09月15日 17:04:10 +00:00Commented Sep 15, 2019 at 17:04
Since JavaEE is now governed by https://jakarta.ee/, the new Maven coordinates as of 2.3.2 are:
https://eclipse-ee4j.github.io/jaxb-ri/#maven-artifacts
The first released jaxb.version 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>
I followed this URL and the below settings had really helped me. I use Java 10 with STS IDE in Macbook Pro. It works like a charm.
<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>
Not an answer, but an addendum: I got because running groovysh
(Groovy 2.4.13) if JAVA_HOME points to a Java 9 installation (java version "9.0.1"
to be precise) fails abysmally:
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 to:
Go to the JAXB Project at github.io ("JAXB is licensed under a dual license - CDDL 1.1 and GPL 2.0 with Class-path Exception")
Download
jaxb-ri-2.3.0.zip
Unzip wherever you put your java infrastructure files (in my case,
/usr/local/java/jaxb-ri/
). Other solution may exist (maybe via SDKMAN, I dunno)Make sure the jars in the lib subdirectory are on the
CLASSPATH
. I do it via a script started on bash startup, called/etc/profile.d/java.sh
, where I added (among many other lines) the following loop:
Packed into a 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!
-
1I don't understand the downvotzes. Apparently people want to faff around with command-line options instead of actually getting the jars? Suit yourself.David Tonhofer– David Tonhofer2018年01月20日 17:50:36 +00:00Commented Jan 20, 2018 at 17:50
-
8Java developers typically use build tools like Gradle or Maven to manage dependencies rather than manually downloading jars. That is probably the reason for the down votes.Joshua Davis– Joshua Davis2018年03月24日 13:55:11 +00:00Commented Mar 24, 2018 at 13:55
I encountered the same issue using Spring Boot 2.0.5.RELEASE
on Java 11.
Adding javax.xml.bind:jaxb-api:2.3.0
alone did not fix the problem. I also had to update Spring Boot to the latest Milestone 2.1.0.M2
, so I assume this will be fixed in the next official release.
-
This does not sound related to me. there are several solutions in this thread that work regardless of using spring boot 2. (I also use spring boot 2.0.5.RELEASE btw). Maybe in Spring 2.1.0.M2 there is already a jaxb runtime included.Sebastian Thees– Sebastian Thees2018年11月02日 09:40:11 +00:00Commented Nov 2, 2018 at 9:40
-
It seems that with Spring Boot 2.1.0.RELEASE, JAXB is not necessary anymore - github.com/spring-projects/spring-boot/releasesBurrich– Burrich2018年11月04日 14:53:54 +00:00Commented Nov 4, 2018 at 14:53
As the official documentation states:
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).
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:
Maven
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
</dependency>
Gradle (build.gradle.kts):
implementation("org.glassfish.jaxb:jaxb-runtime")
Gradle (build.gradle)
implementation 'org.glassfish.jaxb:jaxb-runtime'
If you rather specify a specific version, take a look here: https://mvnrepository.com/artifact/org.glassfish.jaxb/jaxb-runtime
you can use this dependency
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
-
where I add thisSurajkaran Meghwanshi– Surajkaran Meghwanshi2023年01月12日 10:22:39 +00:00Commented Jan 12, 2023 at 10:22
-
you can add this in pom.xml fileAkash Kumar Sahoo– Akash Kumar Sahoo2023年01月13日 15:21:26 +00:00Commented Jan 13, 2023 at 15:21
-
where is this pom.xml file , is I need to create new one @AkashKumarSahooSurajkaran Meghwanshi– Surajkaran Meghwanshi2023年01月16日 07:13:55 +00:00Commented Jan 16, 2023 at 7:13
-
1@SurajkaranMeghwanshi if you are creating a maven project you would have pom.xml in the project foulderAkash Kumar Sahoo– Akash Kumar Sahoo2024年03月24日 18:38:33 +00:00Commented Mar 24, 2024 at 18:38
-
its gone 1 year, but thanks for reply. @AkashKumarSahooSurajkaran Meghwanshi– Surajkaran Meghwanshi2024年04月08日 04:21:13 +00:00Commented Apr 8, 2024 at 4:21
If you are using JDK 11, you can just add the following to your Gradle(app):
dependencies {
...
annotationProcessor "javax.xml.bind:jaxb-api:2.3.1"
...
}
It works for me.
-
ya, this allows us to forget this JAXB , but in some cases seems some elements can not found after this change . ex: class file for android.view.ViewGroup not foundcharitha amarasinghe– charitha amarasinghe2022年12月01日 18:39:13 +00:00Commented Dec 1, 2022 at 18:39
You only need 1 dependency:
dependencies {
implementation ("jakarta.xml.bind:jakarta.xml.bind-api:2.3.2")
-
not worked still showing same errorSurajkaran Meghwanshi– Surajkaran Meghwanshi2023年01月12日 10:22:59 +00:00Commented Jan 12, 2023 at 10:22
OK, I have been having the same kind of issue, but I was using 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 even-though I set the global Java version to 8, as soon as I fixed that it all worked.
For anybody who might have this kind of problem, check out How to fix Maven to use default Java (archived)
If you are using JDK 8 above, follow this steps:
- Add javax.xml.bind dependency
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
- clean maven
- install maven