Issue
My environment:
java -version
openjdk version "11.0.2" 2019-01-15 LTS
OpenJDK Runtime Environment Corretto-11.0.2.9.1 (build 11.0.2+9-LTS)
OpenJDK 64-Bit Server VM Corretto-11.0.2.9.1 (build 11.0.2+9-LTS, mixed mode)
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.1</version>
</dependency>
And a simple program:
public static void main(String[] args) throws JAXBException {
JAXBContext context = JAXBContext.newInstance(Person.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
Person person = new Person(1L, "Thomas");
marshaller.marshal(person, System.out);
}
At this point, everything works just fine.
Then I add module-info.java:
module com.thanh {
requires java.xml;
requires java.xml.bind;
opens com.thanh;
exports com.thanh;
}
And exception:
Exception in thread "main" javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath.
- with linked exception:
[java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory]
at java.xml.bind/javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:278)
at java.xml.bind/javax.xml.bind.ContextFinder.find(ContextFinder.java:421)
at java.xml.bind/javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:721)
at java.xml.bind/javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:662)
at com.thanh/com.thanh.App.main(App.java:12)
Caused by: java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at java.xml.bind/javax.xml.bind.ServiceLoaderUtil.nullSafeLoadClass(ServiceLoaderUtil.java:122)
at java.xml.bind/javax.xml.bind.ServiceLoaderUtil.safeLoadClass(ServiceLoaderUtil.java:155)
at java.xml.bind/javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:276)
... 4 more
How can I get JAXB work with Java 11 module?
I need module for my JavaFX project.
Thank you!
Solution
According to JAXB official page, you should use these dependencies:
<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>
Answered By - ZhekaKozlov
Answer Checked By - Timothy Miller (JavaFixing Admin)