gpt4 book ai didi

java - OSGi 包中访问标准 java 类时出现 NoClassDefFoundError

转载 作者:行者123 更新时间:2023-12-02 14:51:06 29 4
gpt4 key购买 nike

我正在尝试从我的主应用程序访问 OSGI bundle 。但是,如果访问 bundle 中使用 javax.xml.* 包的类。抛出 NoClassDefFoundError。

java.lang.NoClassDefFoundError: javax/xml/transform/Source
at de.foo.bar.test.Builder.<init>(Builder.java:46) ~[test-bundle-1.4.0.jar:1.4.0]
at de.foo.bar.test.request.Factory.createRequest(Factory.java:99) ~[?:?]
at de.foo.bar.test.request.Request.prepareRequest(Request.java:93) ~[?:?]
at de.foo.bar.test.request.Request.process(Request.java:60) ~[?:?]
at de.foo.bar.test.TestClass.execute(TestClass.java:74) ~[?:?]
at de.foo.bar.test.TestClass.execute(TestClass.java:1) ~[?:?]
at de.foo.bar.service.system.OsgiTransactionHandler.handleTransaction(OsgiTransactionHandler.java:71) ~[bin/:?]
at de.foo.bar.service.system.TransactionRequestService.processTransaction(TransactionRequestService.java:110) ~[bin/:?]
at de.foo.bar.service.system.TransactionRequestService.runService(TransactionRequestService.java:72) ~[bin/:?]
at de.foo.bar.service.model.InfiniteService.run(InfiniteService.java:57) ~[bin/:?]
at com.google.common.util.concurrent.AbstractExecutionThreadService$1$2.run(AbstractExecutionThreadService.java:60) [guava-16.0.1.jar:?]
at com.google.common.util.concurrent.Callables$3.run(Callables.java:93) [guava-16.0.1.jar:?]
at java.lang.Thread.run(Thread.java:662) [?:1.6.0_43]
Caused by: java.lang.ClassNotFoundException: javax.xml.transform.Source not found by test-bundle [1]
at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1532) ~[org.apache.felix.framework-4.2.1.jar:?]
at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:75) ~[org.apache.felix.framework-4.2.1.jar:?]
at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1955) ~[org.apache.felix.framework-4.2.1.jar:?]
at java.lang.ClassLoader.loadClass(ClassLoader.java:247) ~[?:1.6.0_43]
... 13 more

我检查了 MANIFEST.MF 中导入的包,可以看到,声明了包 javax.xml.transform(我将其缩短了一点):

Import-Package: com.fasterxml.jackson.core;resolution:=optional,com.fast
...
500;resolution:=optional,javax.servlet;resolution:=optional,javax.sql;r
esolution:=optional,javax.swing;resolution:=optional,javax.xml.bind;res
olution:=optional,javax.xml.bind.annotation;resolution:=optional,javax.
xml.bind.annotation.adapters;resolution:=optional,javax.xml.parsers;res
olution:=optional,javax.xml.transform;resolution:=optional,javax.xml.tr
ansform.stream;resolution:=optional,javax.xml.validation;resolution:=op
tional,org.bson;resolution:=optional,org.lightcouch;resolution:=optiona
l,org.osgi.framework;resolution:=optional;version="[1.5,2)",org.osgi.fr
amework.wiring;resolution:=optional,org.w3c.dom;resolution:=optional,or
g.xml.sax;resolution:=optional,sun.misc;resolution:=optional

我使用 4.2.1 版本中的 apache felix 作为嵌入式框架,maven-bundle-plugin 配置如下:

<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-Name>Test Bundle</Bundle-Name>
<Bundle-Activator>de.foo.bar.test.activator.TestActivator</Bundle-Activator>
<Service-Component>OSGI-INF/component.xml</Service-Component>
<Export-Package>de.foo.bar.test</Export-Package>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Import-Package>*;resolution:=optional</Import-Package>
<Embed-Transitive>true</Embed-Transitive>
<Embed-Dependency>*;scope=compile|runtime;inline=false;artifactId=!org.osgi.core</Embed-Dependency>
<_failok>true</_failok>
<_nouses>true</_nouses>
<_nodefaultversion>true</_nodefaultversion>
<_snapshot>${osgi-version-qualifier}</_snapshot>
</instructions>
</configuration>
</plugin>

我尝试使用 FRAMEWORK_SYSTEMPACKAGES_EXTRA 属性导出丢失的包。但这似乎会导致导出所有 Java 包。我在第 7 个包裹时就停止了。

有没有办法提供这些java标准包而不显式导出它们?

最佳答案

这个问题的原因在于框架配置。根据在线教程,我在配置中添加了以下参数:

config.put(Constants.FRAMEWORK_SYSTEMPACKAGES, "org.osgi.framework; version=1.5.0");

这导致默认的系统包被替换,除了我导出的额外包之外,只有 org.osgi.framework 可用。

删除此行后,默认包再次可用,因此 javax.xml.transform。

关于java - OSGi 包中访问标准 java 类时出现 NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22171570/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com