【问题标题】:Issues with EclipseLink MOXy OSGi bundleEclipseLink MOXy OSGi 包的问题
【发布时间】:2013-04-20 14:23:53
【问题描述】:

我正在开发一个非常简单的 Eclipse 插件,它使用 EclipseLink 的 MOXy 进行 XML 绑定(编组/解组)。当我在插件(Activator.java)的启动过程中尝试编组一个简单的类(Person.java)时,我遇到了几个错误。代码如下:

代码:

Person.java

package test_rcp;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Person {

    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

}

Activator.java

package test_rcp;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

public class Activator extends AbstractUIPlugin {

    public void start(BundleContext context) throws Exception {
        super.start(context);
        plugin = this;

        // Just to make sure EclipseLink is already loaded
        System.out.println(org.eclipse.persistence.Version.getVersion());

        Person p1 = new Person();
        p1.setName("John Dewey");
        p1.setAge(54);
        JAXBContext jc = JAXBContext.newInstance("test_rcp",Person.class.getClassLoader());

         Marshaller marshaller = jc.createMarshaller();
         marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
         marshaller.marshal(p1, System.out);
    }
}

MANIFEST.MF

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Test-RCP
Bundle-SymbolicName: Test-RCP; singleton:=true
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: test_rcp.Activator
Require-Bundle: org.eclipse.ui,
 org.eclipse.core.runtime,
 org.eclipse.persistence.core,
 org.eclipse.persistence.moxy;bundle-version="2.4.1"
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-1.7

test_rcp还包含jaxb.indexjaxb.properties

jaxb.in​​dex

Person

jaxb.properties

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

问题:

但运行插件会引发以下异常:

javax.xml.bind.JAXBException: Provider org.eclipse.persistence.jaxb.JAXBContextFactory could not be instantiated: 
javax.xml.bind.JAXBException: Provider class org.eclipse.persistence.jaxb.JAXBContextFactory could not be instantiated:
 javax.xml.bind.JAXBException:
 ClassCastException: attempting to cast bundleresource://43.fwk583158213/javax/xml/bind/JAXBContext.class to jar:file:/usr/lib/jvm/java-7-oracle/jre/lib/rt.jar!/javax/xml/bind/JAXBContext.class.  Please make sure that you are specifying the proper ClassLoader.

我知道问题是由于两个类加载器已经加载了javax.xml.bind 包,但我不知道为什么以及如何避免这种情况。

Morover,通过使用JAXBContextFactory 而不是JAXBContext.newInstance(建议here),即:

JAXBContext jc = org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(new Class[] {Person.class}, null);

我会得到以下异常:

java.lang.LinkageError: loader constraint violation:
loader (instance of org/eclipse/osgi/internal/baseadaptor/DefaultClassLoader) previously
initiated loading for a different type with name "javax/xml/bind/JAXBContext"

我已经尝试了以下解决方案,但没有成功:

https://stackoverflow.com/a/12943115/2295964

https://stackoverflow.com/a/11797373/2295964

如果有人能帮助我摆脱这种痛苦,我将不胜感激!

【问题讨论】:

    标签: java jaxb osgi equinox


    【解决方案1】:

    您发布的清单仅包含Require-Bundle

    Require-Bundle: org.eclipse.ui,
     org.eclipse.core.runtime,
     org.eclipse.persistence.core,
     org.eclipse.persistence.moxy;bundle-version="2.4.1"
    

    我建议将Import-Package: javax.xml.bind 添加到捆绑清单中。这应该允许框架将包解析为org.eclipse.persistence.moxy 使用的包。

    【讨论】:

    • 是的,在@blaise-doughan 的回答之后不久,我发现我错过了“javax.xml.bind”,但是只将它添加到 MANIFEST.MF 的顶部就可以了。
    【解决方案2】:

    您需要确保导入 javax.xml.bind 包,而不是从 JRE 获取 JAXB API。

    【讨论】:

    • 我一直在使用最新版本的 EclipseLink (javax.xml.bind_2.2.0.v201105210648.jar) 提供的捆绑包。即使在将它添加到 MANIFEST.MF 之后,我也会收到相同的类转换错误:Eclipse 似乎正在从 JRE 加载默认值。
    猜你喜欢
    • 2012-08-09
    • 1970-01-01
    • 2017-07-29
    • 1970-01-01
    • 2011-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    相关资源
    最近更新 更多