飞翔的打火机 · 给已经创建的容器开放端口号_docker ...· 2 周前 · |
含蓄的消防车 · Java中的Json对象转换为Json数组· 6 月前 · |
要出家的钥匙扣 · MySql存储过程循环的使用 - 掘金· 1 年前 · |
眼睛小的登山鞋 · Select Table Row in ...· 1 年前 · |
爱笑的高山 · thinkphp断线重连-阿里云开发者社区· 1 年前 · |
我有一个小项目,当我让它运行时,它确实抛出了一个异常。
问题在这里(TestObject.java):
final JAXBContext jaxbContext = JAXBContext.newInstance(...);
我真的不明白为什么这会抛出异常。我还创建了一个像咒语一样工作的测试(TestTest.java)。请原谅我的名字,但这只是一个小的测试应用程序,让它与java 11一起工作。
我遵循了这个: javax.xml.bind.JAXBException Implementation of JAXB-API has not been found on module path or classpath 并添加了
compile('javax.xml.bind:jaxb-api:2.3.0')
compile('javax.activation:activation:1.1')
compile('org.glassfish.jaxb:jaxb-runtime:2.3.0')
发送到我的build.gradle,但它却抱怨说
Error:java: module not found: java.activation
都不见了。因为这也从Java 11中删除了,所以我添加了:
compile 'com.sun.activation:javax.activation:1.2.0'
但是它抛出了很多错误:
Error:java: the unnamed module reads package com.sun.xml.bind from both jaxb.runtime and jaxb.core
Error:java: the unnamed module reads package com.sun.xml.bind.util from both jaxb.runtime and jaxb.core
Error:java: the unnamed module reads package com.sun.xml.bind.marshaller from both jaxb.runtime and jaxb.core
Error:java: the unnamed module reads package com.sun.xml.bind.api from both jaxb.runtime and jaxb.core
Error:java: the unnamed module reads package com.sun.xml.bind.v2 from both jaxb.runtime and jaxb.core
Error:java: the unnamed module reads package com.sun.xml.bind.v2.util from both jaxb.runtime and jaxb.core
Error:java: the unnamed module reads package com.sun.xml.bind.v2.model.impl from both jaxb.runtime and jaxb.core
Error:java: the unnamed module reads package com.sun.xml.bind.v2.model.annotation from both jaxb.runtime and jaxb.core
Error:java: the unnamed module reads package com.sun.xml.bind.v2.runtime from both jaxb.runtime and jaxb.core
Error:java: the unnamed module reads package com.sun.xml.bind.v2.runtime.unmarshaller from both jaxb.runtime and jaxb.core
Error:java: the unnamed module reads package com.sun.xml.bind.unmarshaller from both jaxb.runtime and jaxb.core
Error:java: module jaxb.runtime reads package com.sun.xml.bind from both jaxb.core and jaxb.runtime
Error:java: module jaxb.runtime reads package com.sun.xml.bind.util from both jaxb.core and jaxb.runtime
Error:java: module jaxb.runtime reads package com.sun.xml.bind.marshaller from both jaxb.core and jaxb.runtime
Error:java: module jaxb.runtime reads package com.sun.xml.bind.api from both jaxb.core and jaxb.runtime
Error:java: module jaxb.runtime reads package com.sun.xml.bind.v2 from both jaxb.core and jaxb.runtime
Error:java: module jaxb.runtime reads package com.sun.xml.bind.v2.util from both jaxb.core and jaxb.runtime
Error:java: module jaxb.runtime reads package com.sun.xml.bind.v2.model.impl from both jaxb.core and jaxb.runtime
Error:java: module jaxb.runtime reads package com.sun.xml.bind.v2.model.annotation from both jaxb.core and jaxb.runtime
Error:java: module jaxb.runtime reads package com.sun.xml.bind.v2.runtime from both jaxb.core and jaxb.runtime
Error:java: module jaxb.runtime reads package com.sun.xml.bind.v2.runtime.unmarshaller from both jaxb.core and jaxb.runtime
Error:java: module jaxb.runtime reads package com.sun.xml.bind.unmarshaller from both jaxb.core and jaxb.runtime
Error:java: module jaxb.core reads package com.sun.xml.bind from both jaxb.core and jaxb.runtime
让我感到困惑的是:测试可以正常工作。因此,在junit5环境中似乎存在某种jaxb实现。我还在如何将java 8应用程序迁移到java 9模块中找到了一些其他信息,其中我找到了以下内容:
compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.4.0-b180830.0359'
compile group: 'org.glassfish.jaxb', name: 'jaxb-runtime', version: '2.4.0-b180830.0438'
compile group: 'org.glassfish.jaxb', name: 'jaxb-xjc', version: '2.4.0-b180830.0438'
但这也不起作用。如果有人能帮我解决这个问题,我会很高兴的。我在64位的Fedora 29上使用java-openjdk-11.0.1.13-11.rolling.fc29.x86_64。
附言:我想把所有这些都写进主题为"javax.xml.bind.JAXBException...“的帖子的评论中。但他们不允许我这么做。所以我创建了这个新帖子。
edit:在该示例应用程序中抛出的异常如下:
Exception in thread "main" java.lang.RuntimeException: 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.bind.v2.ContextFactory]
at test/com.example.TestObject.doSomething(TestObject.java:21)
at test/com.example.TestObject.main(TestObject.java:12)
Caused by: 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.bind.v2.ContextFactory]
at java.xml.bind/javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:269)
at java.xml.bind/javax.xml.bind.ContextFinder.find(ContextFinder.java:412)
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 test/com.example.TestObject.doSomething(TestObject.java:17)
... 1 more
Caused by: java.lang.ClassNotFoundException: com.sun.xml.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:267)
... 5 more
发布于 2019-02-13 20:06:36
解决方案非常简单。首先,使用jaxb 2.3.1。测试版的东西只用于测试。真正的问题是模块-info.java。jaxb需要有两个条目:
requires java.xml.bind;
requires com.sun.xml.bind;
第一个requires定义实现,第二个定义API。
唯一需要的两个依赖项是:
compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.1'
compile group: 'org.glassfish.jaxb', name: 'jaxb-runtime', version: '2.3.1'
发布于 2021-02-06 01:41:40
为了完成其他答案:
JAXB使用绑定到当前线程的类加载器来查找JAXB
ContextFactory
类(
Thread.currentThread().getContextClassLoader()
),并且在某些情况下(例如使用jetty线程),绑定的类加载器是一个
PlatformClassLoader
,这具有
不知道类路径
的特殊性,这与
AppClassLoader
相反。
要解决此问题,JAXB和运行时必须位于
模块路径
(可由
PlatformClassLoader
访问)中。
例如,app打包了以下依赖关系:
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.3</version>
</dependency>
因此,应该使用
jaxb-runtime
及其模块依赖项启动应用程序,如下所示:
### Set the module-path
MODULEPATH="./lib/jaxb-runtime-2.3.3.jar"
含蓄的消防车 · Java中的Json对象转换为Json数组 6 月前 |
要出家的钥匙扣 · MySql存储过程循环的使用 - 掘金 1 年前 |
爱笑的高山 · thinkphp断线重连-阿里云开发者社区 1 年前 |