How to Resolve the 'javax.xml.bind Package Does Not Exist' Error in Java 11?

Question

How do I resolve the 'javax.xml.bind package does not exist' compilation error when using JAXB in Java 11?

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();
}

Answer

In Java 11, the Java EE modules, including JAXB (Java Architecture for XML Binding), have been removed from the standard Java SE environment, which leads to the 'javax.xml.bind package does not exist' error when compiling. Here's how to resolve it.

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

Causes

  • Java 11 no longer includes JAXB as part of the core Java libraries.
  • The JAXB API was part of Java SE in earlier versions (like Java 8) but was moved to external libraries in Java 11.
  • Using JAXB without the necessary dependencies will result in compilation errors.

Solutions

  • Add the JAXB API as a dependency in your project. If you are using Maven, you can include it in your `pom.xml` file by adding the following dependency:
  • Add the JAXB implementation to your project. For Maven, include this dependency in your `pom.xml`:
  • For Gradle users, you can add the following dependencies in your `build.gradle` file:
  • Ensure your IDE's project structure recognizes the added dependencies correctly, and refresh the project.

Common Mistakes

Mistake: Not including both JAXB API and its implementation in the dependencies.

Solution: Make sure to add both the JAXB API and the JAXB runtime implementation to your project dependencies.

Mistake: Forgetting to refresh the project after adding dependencies in the IDE.

Solution: After modifying `pom.xml` or `build.gradle`, ensure to refresh or rebuild your project in the IDE.

Helpers

  • Java 11 JAXB
  • javax.xml.bind package error
  • fix JAXB error Java 11
  • JAXB deserialization Java 11
  • JAXB dependencies Maven
  • Java XML binding error 2023

Related Questions

⦿Can You Use the instanceof Operator in a Switch Statement in Java?

Learn if you can use the instanceof operator in a Java switch statement and explore alternatives with code examples.

⦿How to Convert a Byte Array to a String and Back in Java?

Learn how to accurately convert byte arrays to strings and back in Java including handling negative byte values for correct conversions.

⦿Understanding the Differences Between Java System Properties and Environment Variables

Explore the key differences between Java system properties System.getProperties and environment variables System.getenv in the JVM.

⦿How to Retrieve the User's Home Directory in Java Across All Platforms?

Learn how to find the users home directory in Java with crossplatform compatibility. Example code included for Windows OS X and Linux.

⦿How to Prevent Constructor Overload in Dependency Injection?

Learn effective strategies to manage constructor overload in Dependency Injection using best practices for IoC.

⦿How to Define a Java 8 Lambda Function Without Arguments or Return Type?

Learn how to simplify Java 8 lambda functions using functional interfaces without unnecessary Void type parameters for cleaner code.

⦿Is It Bad Practice to Call System.gc() in Java?

Discover why manually calling System.gc is often discouraged in Java programming and understand when if ever it might be acceptable.

⦿Understanding Java Heap Memory: Young, Old, and Permanent Generations

Explore the concepts of Young Old and Permanent Generations in Java heap memory management and their interactions for optimized performance.

⦿How to Create a Deep Copy of an Object in JavaScript?

Learn how to implement a deep copy function in JavaScript ensuring the original object and clone share no references.

⦿What is the Difference Between StringUtils.isBlank() and String.isEmpty()?

Learn the key differences between StringUtils.isBlank from Apache Commons Lang and String.isEmpty from Java including their use cases and behavior.

© Copyright 2025 - CodingTechRoom.com