379

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?

2
  • 1
    @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. Commented Oct 10, 2019 at 13:58
  • 2
    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" Commented Apr 19, 2021 at 17:55

1 Answer 1

741

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>
Sign up to request clarification or add additional context in comments.

21 Comments

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.
I had to add the above dependencies and remove the switch --add-modules=java.xml.bind from the maven compiler plugin.
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: mvnrepository.com/artifact/javax.xml.bind
@ivan_drago can you try to add jaxb-runtime dependency? <dependency> <groupId>org.glassfish.jaxb</groupId> <artifactId>jaxb-runtime</artifactId> <version>2.3.2</version> </dependency>
@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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.