347

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?

Boris
  • 22,667
  • 16
  • 50
  • 71
  • @Wolfgang, with such views on software evolution we would have stayed in Java 1.1 era. I can't totally agree with the opinion expressed in your wiki. Yes, they broke the backward compatibility promise, but only to move with the time. – Boris Oct 10 '19 at 13:58
  • 1
    I've got the error "package javax.xml.bind does not exist" when running a maven build with maven.compiler.source=1.8 or version.java.source=1.8 in poms but java 11 set on the console. Fix by setting your java_home local env variable to the Java 8 SDK: "set java_home=path_to_java_8" – electrobabe Apr 19 '21 at 17:55

1 Answers1

677

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 Jakarta XML Binding 3.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

Jakarta EE 10 update (Jun 2022)

Use latest release of Jakarta XML Binding 4.0 (requires Java SE 11 or newer):

<dependency>
  <groupId>jakarta.xml.bind</groupId>
  <artifactId>jakarta.xml.bind-api</artifactId>
  <version>4.0.0</version>
</dependency>
<dependency>
  <groupId>com.sun.xml.bind</groupId>
  <artifactId>jaxb-impl</artifactId>
  <version>4.0.0</version>
  <scope>runtime</scope>
</dependency>
Boris
  • 22,667
  • 16
  • 50
  • 71
  • 2
    I'm upgrading a Spring Boot 2.1.0 application to Java 11, but every time I start the JAR it outputs: Error occurred during initialization of boot layer java.lang.module.FindException: Module java.xml.bind not found. I added those three dependencies, but no luck. Anything else required or things to check? I'm running java 11.0.1. – wsams Jan 03 '19 at 19:44
  • @wsams are you using [Java modules](https://openjdk.java.net/projects/jigsaw/quick-start)? Have you updated the module declaration (module-info.java)? – Boris Jan 04 '19 at 07:41
  • 1
    I had to add the above dependencies and remove the switch _--add-modules=java.xml.bind_ from the maven compiler plugin. – Lars Behnke Feb 19 '19 at 15:01
  • @LarsBehnke what did your maven command look like? How do you add the switch? – Robbo_UK Mar 13 '19 at 12:05
  • 4
    Versions are incorrect. Latest versions up to date are: jaxb-api:2.3.1 jaxb-core:2.2.11 jaxb-impl:2.0.2 You can check latest versions at: https://mvnrepository.com/artifact/javax.xml.bind – Juan García Oct 07 '19 at 21:07
  • @kap, thanks for pointing this out, I have updated the answer with Jakarta EE 8. – Boris Mar 25 '20 at 15:18
  • @Boris - So using the Jakarta maven dependency allowed me successfully execute `mvn generate-sources` on my project. However when I run `mvn install` I run into build errors like the following: `javax.xml.bind.annotation does not exist`. Any thoughts? –  Apr 15 '20 at 18:54
  • 2
    @ivan_drago can you try to add [jaxb-runtime](https://search.maven.org/artifact/org.glassfish.jaxb/jaxb-runtime) dependency? ` org.glassfish.jaxb jaxb-runtime 2.3.2 ` – Boris Apr 15 '20 at 19:54
  • @Boris - So I added that in and now I'm getting the following error: `Caused by: java.lang.NoSuchFieldError: REFLECTION` when I run `mvn generate-sources`. Looks like that last dependency I added is in conflict with another lib that is being injected elsewhere...? –  Apr 15 '20 at 20:31
  • Is Jakarta EE 8 compatible with Java 10 and Java 11? – Bionix1441 Oct 14 '20 at 13:16
  • @Bionix1441 Java 10 is [EOL](https://www.oracle.com/java/technologies/java-se-support-roadmap.html#JavaSESupport). It is compatible with Java 11, [here](https://www.eclipse.org/community/eclipse_newsletter/2019/november/1.php) is an example how to bootstrap a Jakarta EE 8 Maven project. – Boris Oct 14 '20 at 15:51
  • 1
    @Graham Leggett, thanks for pointing this out, I have updated the answer with Jakarta EE 8 compatible implementation dependency. Also updated the answer with Jakarta EE 9. – Boris Nov 20 '20 at 10:32
  • switch to java 1.8.0_261 as 1.8.271.09 gives issues – Devharsh Trivedi Dec 07 '20 at 19:04
  • Where do you add those things on a Mac? – Nikowhy Jan 11 '21 at 11:47
  • That's correct, I had Java 11 on my home path, switched to 8 and managed to build my project (using maven). Additional note: I downgraded 11 to 8 also in VS Code but kept the runtime pointing to 11 because it's required by the IDE. This way I built the project, got rid of VS Code errors, and still able to use Code code for development. – Cortex Mar 06 '21 at 00:50
  • At first, I couldn't resolve jakarta library in gradle. Adding following repository helped, url = 'https://repo1.maven.org/maven2' this may change later on – Vortex Mar 08 '21 at 01:18
  • 1
    @fresko the latest Java version is not 8, it's 16. See [Oracle Java SE Support Roadmap](https://www.oracle.com/java/technologies/java-se-support-roadmap.html) for more details. – Boris Jun 23 '21 at 15:57
  • @Boris I use JakartaEE 8 and I followed your instructions above add added dependencies as you specified under "JakartaEE Update (March 2020)". It works but inside the code, all reference to javax.xml.transform, such as Result, Source, Transformer, TransformerException, TransformerFactory, SAXResult are red underlined. Howering over it says "The package javax.xml.transform" is accessible from more than one module. – pixel Sep 27 '21 at 21:59
  • @pixel sounds like an [issue with Eclipse IDE](https://stackoverflow.com/q/55571046/3301492), is it right? Perhaps you could share a link to your `pom.xml` to anlyse further? – Boris Sep 28 '21 at 13:11