JAXB 和 Java 11 模块-info.java
JAXB and Java 11 module-info.java
我的环境:
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)
Maven 依赖项:
<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>
还有一个简单的程序:
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);
}
此时,一切工作正常。
然后我添加module-info.java:
module com.thanh {
requires java.xml;
requires java.xml.bind;
opens com.thanh;
exports com.thanh;
}
例外情况:
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
如何让 JAXB 与 Java 11 模块一起工作?
我的 JavaFX 项目需要模块。
谢谢!
根据 JAXB official page,您应该使用这些依赖项:
<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>
我的环境:
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)
Maven 依赖项:
<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>
还有一个简单的程序:
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);
}
此时,一切工作正常。
然后我添加module-info.java:
module com.thanh {
requires java.xml;
requires java.xml.bind;
opens com.thanh;
exports com.thanh;
}
例外情况:
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
如何让 JAXB 与 Java 11 模块一起工作?
我的 JavaFX 项目需要模块。
谢谢!
根据 JAXB official page,您应该使用这些依赖项:
<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>