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

Ответов: 1

Ответы (1)

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:


  javax.xml.bind
  jaxb-api
  2.3.0


  com.sun.xml.bind
  jaxb-core
  2.3.0


  com.sun.xml.bind
  jaxb-impl
  2.3.0

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:


  jakarta.xml.bind
  jakarta.xml.bind-api
  2.3.3


  com.sun.xml.bind
  jaxb-impl
  2.3.3
  runtime

Jakarta EE 9 update (Nov 2020)

Use latest release of Jakarta XML Binding 3.0:


  jakarta.xml.bind
  jakarta.xml.bind-api
  3.0.0


  com.sun.xml.bind
  jaxb-impl
  3.0.0
  runtime

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

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

2022 WebDevInsider