Home > Java > javaTutorial > How to Resolve the 'javax.xml.bind does not exist' Error in Java 11 When Using JAXB?

How to Resolve the 'javax.xml.bind does not exist' Error in Java 11 When Using JAXB?

Barbara Streisand
Release: 2024-11-06 00:47:02
Original
469 people have browsed it

How to Resolve the

The javax.xml.bind package does not exist in Java 11

When using JAXB to parse XML data and authenticate it:

<code class="java">JAXBContext context = JAXBContext.newInstance("com.acme.foo");
Unmarshaller unmarshaller = context.createUnmarshaller();
unmarshaller.setSchema(schema);
FooObject fooObj = (FooObject) unmarshaller.unmarshal(new File("foo.xml"));</code>
Copy after login

Based on Java 8, this code runs fine, but in Java 11, the following compilation error occurs:

package javax.xml.bind does not exist
Copy after login

Workaround

According to the release notes, Java 11 removed Java EE modules, including javax.xml.bind (JAXB), which caused this issue.

To resolve this issue, you need to use an alternative version of Java EE technology, just add a Maven dependency containing the required classes:

<code class="xml"><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></code>
Copy after login

Jakarta EE 8 Update (2020 March 2020)

You can now replace the old JAXB module with Jakarta XML Binding in Jakarta EE 8:

<code class="xml"><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></code>
Copy after login

Jakarta EE 9 Update (November 2020) Month)

Use the latest version of Jakarta XML Binding 3.0:

<code class="xml"><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></code>
Copy after login

Note: With Jakarta EE 9 adopting the new API package namespace jakarta .xml.bind.*, please update the import statement:

<code class="java">javax.xml.bind -> jakarta.xml.bind</code>
Copy after login

Jakarta EE 10 Update (June 2022)

Use the latest version of Jakarta XML Binding 4.0 (requires Java SE 11 or higher):

<code class="xml"><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></code>
Copy after login

The above is the detailed content of How to Resolve the 'javax.xml.bind does not exist' Error in Java 11 When Using JAXB?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template