在OSGi环境中使用Xerces的NoClassDefFoundError

时间:2016-04-21 15:30:22

标签: osgi apache-karaf xerces apache-servicemix

我已经构建了一个实例化org.apache.xerces.util.XMLCatalogResolver类的简单包。 在我的pom中,我有这些依赖项和插件:

<build>
        <plugins>
            <!-- Plugin to create bundle -->
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>2.5.4</version>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Import-Package>*</Import-Package>
                        <Export-Package>myXMLResolver</Export-Package>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-core</artifactId>
            <version>2.16.1</version>
            <type>bundle</type>
            <scope>provided</scope>
        </dependency>
        <!-- xerces is provided in /lib/endorsed/ folder of ServiceMix -->
        <dependency>
            <groupId>xerces</groupId>
            <artifactId>xercesImpl</artifactId>
            <version>2.11.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.servicemix.bundles</groupId>
           <artifactId>org.apache.servicemix.bundles.xmlresolver</artifactId>
            <version>1.2_5</version>
            <type>bundle</type>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</build>

在“本地”(在IDE中)测试捆绑包工作正常。

将其部署到 / deploy 文件夹中也没问题(其状态为“活动”)。

运行它时(通过Camel路由),我得到了这个堆栈跟踪:

Caused by: java.lang.NoClassDefFoundError: org/apache/xml/resolver/CatalogManager
      at org.apache.xerces.util.XMLCatalogResolver.init(Unknown Source)[:]
      at org.apache.xerces.util.XMLCatalogResolver.<init>(Unknown Source)[:]
      at org.apache.xerces.util.XMLCatalogResolver.<init>(Unknown Source)[:]
      at myXMLResolver.MainBean.run(MainBean.java:17)[241:test.myXMLResolver:0.0.1.SNAPSHOT]
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)[:1.7.0_95]
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)[:1.7.0_95]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[:1.7.0_95]
      at java.lang.reflect.Method.invoke(Method.java:606)[:1.7.0_95]
      at org.apache.camel.component.bean.MethodInfo.invoke(MethodInfo.java:408)[199:org.apache.camel.camel-core:2.16.1]
      at org.apache.camel.component.bean.MethodInfo$1.doProceed(MethodInfo.java:279)[199:org.apache.camel.camel-core:2.16.1]
      at org.apache.camel.component.bean.MethodInfo$1.proceed(MethodInfo.java:252)[199:org.apache.camel.camel-core:2.16.1]
      ... 19 more

如果我没错,编译过程中存在来自包CatalogManager的类org.apache.xml.resolver,但不是在运行时。因此,我将org.apache.servicemix.bundles.xmlresolver依赖项部署到 / deploy 文件夹中。 当在Karaf控制台中执行命令bundle:headers <xmlresolver_bundle_id>时,我得到:

Export-Package = 
...
org.apache.xml.resolver;uses:="org.apache.xml.resolver.helpers,javax.xml.parsers,org.apache.xml.resolver.readers";version=1.2,
...

然而,出现相同的堆栈跟踪。 即使我在org.apache.xml.resolver中添加了包<Import-Package>,我也会收到同样的错误。

知道发生了什么事吗?

我正在使用在Java 7上运行的ServiceMix 6.1.0(w / Karaf 3.0.5)。

修改

这是捆绑的MANIFEST.MF:

Manifest-Version: 1.0
Bnd-LastModified: 1461311084908
Build-Jdk: 1.8.0_72-internal
Bundle-ManifestVersion: 2
Bundle-Name: myXMLResolver
Bundle-SymbolicName: test.myXMLResolver
Bundle-Version: 0.0.1.SNAPSHOT
Created-By: Apache Maven Bundle Plugin
Export-Package: myXMLResolver;version="0.0.1";uses:="org.apache.camel,org.apache.xerces.util"
Import-Package: org.apache.camel;version="[2.16,3)",org.apache.xerces.util;version="[2.11,3)"
Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.7))"
Tool: Bnd-2.4.1.201501161923

如果我在org.apache.xml.resolver中添加了包<Import-Package>,则来自MANIFEST的Import-Package成为:org.apache.camel;version="[2.16,3)",org.apache.xerces.util;version="[2.11,3)",org.apache.xml.resolver;version="[1.2,2)"

编辑#2 :解决方法

可以解决方法:

  1. 将捆绑org.apache.servicemix.bundles.xmlresolver放入 <SMX_HOME>/lib/endorsed文件夹
  2. 在文件<SMX_HOME>/etc/config.properties中,添加 org.apache.xml.resolver到酒店 org.osgi.framework.system.package.extra
  3. 但它看起来不像一个干净的方法......

    编辑#3 :在Jira上打开了问题:https://issues.apache.org/jira/browse/SM-3020

0 个答案:

没有答案