[java] Java 11 package javax.xml.bind does not exist

I'm trying to deserialize XML data into a Java content tree using JAXB, validating the XML data as it is unmarshalled:

try {
  JAXBContext context = JAXBContext.newInstance("com.acme.foo");
  Unmarshaller unmarshaller = context.createUnmarshaller();
  unmarshaller.setSchema(schema);
  FooObject fooObj = (FooObject) unmarshaller.unmarshal(new File("foo.xml"));
} catch (UnmarshalException ex) {
  ex.printStackTrace();
} catch (JAXBException ex) {
  ex.printStackTrace();
}

When I build the project with Java 8 it's fine, but building it with Java 11 fails with a compilation error:

package javax.xml.bind does not exist

How do I fix the issue?

This question is related to java jakarta-ee jaxb java-11

The answer is


According to the release-notes, Java 11 removed the Java EE modules:

java.xml.bind (JAXB) - REMOVED
  • Java 8 - OK
  • Java 9 - DEPRECATED
  • Java 10 - DEPRECATED
  • Java 11 - REMOVED

See JEP 320 for more info.

You can fix the issue by using alternate versions of the Java EE technologies. Simply add Maven dependencies that contain the classes you need:

<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-core</artifactId>
  <version>2.3.0</version>
</dependency>
<dependency>
  <groupId>com.sun.xml.bind</groupId>
  <artifactId>jaxb-impl</artifactId>
  <version>2.3.0</version>
</dependency>

Jakarta EE 8 update (Mar 2020)

Instead of using old JAXB modules you can fix the issue by using Jakarta XML Binding from Jakarta EE 8:

<dependency>
  <groupId>jakarta.xml.bind</groupId>
  <artifactId>jakarta.xml.bind-api</artifactId>
  <version>2.3.3</version>
</dependency>
<dependency>
  <groupId>com.sun.xml.bind</groupId>
  <artifactId>jaxb-impl</artifactId>
  <version>2.3.3</version>
  <scope>runtime</scope>
</dependency>

Jakarta EE 9 update (Nov 2020)

Use latest release of Eclipse Implementation of JAXB 3.0.0:

<dependency>
  <groupId>jakarta.xml.bind</groupId>
  <artifactId>jakarta.xml.bind-api</artifactId>
  <version>3.0.0</version>
</dependency>
<dependency>
  <groupId>com.sun.xml.bind</groupId>
  <artifactId>jaxb-impl</artifactId>
  <version>3.0.0</version>
  <scope>runtime</scope>
</dependency>

Note: Jakarta EE 9 adopts new API package namespace jakarta.xml.bind.*, so update import statements:

javax.xml.bind -> jakarta.xml.bind

Questions with java tag:

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error Instantiating a generic type When to create variables (memory management) java doesn't run if structure inside of onclick listener String method cannot be found in a main class method Are all Spring Framework Java Configuration injection examples buggy? Calling another method java GUI I need to know how to get my program to output the word i typed in and also the new rearranged word using a 2D array Java and unlimited decimal places? Read input from a JOptionPane.showInputDialog box Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable Two Page Login with Spring Security 3.2.x Hadoop MapReduce: Strange Result when Storing Previous Value in Memory in a Reduce Class (Java) Got a NumberFormatException while trying to parse a text file for objects Best way for storing Java application name and version properties Call japplet from jframe FragmentActivity to Fragment Comparing two joda DateTime instances Maven dependencies are failing with a 501 error IntelliJ: Error:java: error: release version 5 not supported Has been compiled by a more recent version of the Java Runtime (class file version 57.0) Why am I getting Unknown error in line 1 of pom.xml? Gradle: Could not determine java version from '11.0.2' Error: Java: invalid target release: 11 - IntelliJ IDEA Android Gradle 5.0 Update:Cause: org.jetbrains.plugins.gradle.tooling.util Why is 2 * (i * i) faster than 2 * i * i in Java? must declare a named package eclipse because this compilation unit is associated to the named module How do I install Java on Mac OSX allowing version switching? How to install JDK 11 under Ubuntu? Java 11 package javax.xml.bind does not exist IntelliJ can't recognize JavaFX 11 with OpenJDK 11 Difference between OpenJDK and Adoptium/AdoptOpenJDK OpenJDK8 for windows How to allow all Network connection types HTTP and HTTPS in Android (9) Pie? Find the smallest positive integer that does not occur in a given sequence Error: JavaFX runtime components are missing, and are required to run this application with JDK 11 How to uninstall Eclipse? Failed to resolve: com.google.firebase:firebase-core:16.0.1 How to resolve Unable to load authentication plugin 'caching_sha2_password' issue

Questions with jakarta-ee tag:

Java 11 package javax.xml.bind does not exist javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure The type java.io.ObjectInputStream cannot be resolved. It is indirectly referenced from required .class files Deploying Maven project throws java.util.zip.ZipException: invalid LOC header (bad signature) web.xml is missing and <failOnMissingWebXml> is set to true WELD-001408: Unsatisfied dependencies for type Customer with qualifiers @Default Name [jdbc/mydb] is not bound in this Context An internal error occurred during: "Updating Maven Project". java.lang.NullPointerException How to consume a SOAP web service in Java java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer javax.naming.NameNotFoundException: Name is not bound in this Context. Unable to find javax.naming.NoInitialContextException - Java Session TimeOut in web.xml Another Repeated column in mapping for entity error UnsupportedClassVersionError: JVMCFRE003 bad major version in WebSphere AS 7 Eclipse add Tomcat 7 blank server name ServletException, HttpServletResponse and HttpServletRequest cannot be resolved to a type How to create and handle composite primary key in JPA Intellij IDEA Java classes not auto compiling on save Difference between web server, web container and application server Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.5 or one of its dependencies could not be resolved no sqljdbc_auth in java.library.path deleted object would be re-saved by cascade (remove deleted object from associations) Cannot create JDBC driver of class ' ' for connect URL 'null' : I do not understand this exception How to generate JAXB classes from XSD? PowerMockito mock single static method and return object What's the difference between JPA and Hibernate? How to set up JAX-RS Application using annotations only (no web.xml)? Can someone explain mappedBy in JPA and Hibernate? The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application How to connect with Java into Active Directory Should I use @EJB or @Inject java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.encodeBase64String() in Java EE application What is Java Servlet? Installing J2EE into existing eclipse IDE Java Web Service client basic authentication How to define servlet filter order of execution using annotations in WAR How to force browser to download file? Hibernate Criteria for Dates I don't have "Dynamic Web Project" option in Eclipse new Project wizard Illegal access: this web application instance has been stopped already Why do we use web.xml? APR based Apache Tomcat Native library was not found on the java.library.path? WARNING: Setting property 'source' to 'org.eclipse.jst.jee.server:appname' did not find a matching property What is the difference between Eclipse for Java (EE) Developers and Eclipse Classic? javax.naming.NameNotFoundException Please explain about insertable=false and updatable=false in reference to the JPA @Column annotation Address already in use: JVM_Bind JPA: difference between @JoinColumn and @PrimaryKeyJoinColumn? Why use @PostConstruct?

Questions with jaxb tag:

Java 11 package javax.xml.bind does not exist Java: How to resolve java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException Convert Java object to XML string no suitable HttpMessageConverter found for response type javax.xml.bind.UnmarshalException: unexpected element. Expected elements are (none) java.lang.VerifyError: Expecting a stackmap frame at branch target JDK 1.7 javax.xml.bind.JAXBException: Class *** nor any of its super class is known to this context How to generate JAXB classes from XSD? convert xml to java object using jaxb (unmarshal) I can't understand why this JAXB IllegalAnnotationException is thrown JAXB :Need Namespace Prefix to all the elements Jaxb, Class has two properties of the same name XML element with attribute and content using JAXB Use JAXB to create Object from XML String javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"Group") JAXB: how to marshall map into <key>value</key> JSON Structure for List of Objects Simple conversion between java.util.Date and XMLGregorianCalendar JAXB Exception: Class not known to this context How can I tell jaxb / Maven to generate multiple schema packages? Using JAXB to unmarshal/marshal a List<String> How do I instantiate a JAXBElement<String> object? No @XmlRootElement generated by JAXB Remove 'standalone="yes"' from generated XML JAXB: How to ignore namespace during unmarshalling XML document?

Questions with java-11 tag:

Error: Java: invalid target release: 11 - IntelliJ IDEA How to install OpenJDK 11 on Windows? How to install JDK 11 under Ubuntu? Java 11 package javax.xml.bind does not exist IntelliJ can't recognize JavaFX 11 with OpenJDK 11 Unable to compile simple Java 10 / Java 11 project with Maven Java: How to resolve java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException