"duplicate class definition bug occured" 在 jboss 中使用 jaxb

"duplicate class definition bug occured" using jaxb in jboss

我在 jboss 5.0.1GA 容器中使用 jaxb,在启动时看到一个奇怪的异常。只出现一次。

从堆栈跟踪可以看出,它是在 FrameworkUtil class 的 class 初始化期间。

FrameworkUtil 第 119 行如下所示:

JAXBContext j = JAXBContext.newInstance(Validate.class, Response.class ....<more classes>);

Validate.java 里面有这个:

@XmlRootElement(name="validate")
public class Validate {

    List<String> userGroups;

    @XmlElementWrapper(name="userGroups")
    @XmlElement(name="item")
    public String[] getUserGroups() {

        if (userGroups != null) {
            return userGroups.toArray(new String[userGroups.size()]);
        }
        else return null;
    }

    public void setUserGroups(String[] userGrps) {
        userGroups = new ArrayList<String>();

        if (userGrps != null) {
            for (String userGrp : userGrps) {
                userGroups.add(new String(userGrp));
            }
        }
    }
}

在堆栈跟踪中是对 Validate 的引用。但有时堆栈跟踪是不同的,而是指 Response.class 中的相同字段 (userGroups)。

这是堆栈跟踪:

Mar 19, 2015 11:20:50 AM com.sun.xml.bind.v2.runtime.reflect.opt.Injector inject
WARNING: duplicate class definition bug occured? Please report this : com/mycompany/ecommerce/message/beans/Validate$JaxbAccessorM_getUserGroups_setUserGroups_[Ljava_lang_String;
java.lang.ClassFormatError: Illegal class name "com/mycompany/ecommerce/message/beans/Validate$JaxbAccessorM_getUserGroups_setUserGroups_[Ljava_lang_String;" in class file com/mycompany/ecommerce/messa
ge/beans/Validate$JaxbAccessorM_getUserGroups_setUserGroups_[Ljava_lang_String;
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:643)
        at sun.reflect.GeneratedMethodAccessor209.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at com.sun.xml.bind.v2.runtime.reflect.opt.Injector.inject(Injector.java:205)
        at com.sun.xml.bind.v2.runtime.reflect.opt.Injector.inject(Injector.java:85)
        at com.sun.xml.bind.v2.runtime.reflect.opt.AccessorInjector.prepare(AccessorInjector.java:89)
        at com.sun.xml.bind.v2.runtime.reflect.opt.OptimizedAccessorFactory.get(OptimizedAccessorFactory.java:114)
        at com.sun.xml.bind.v2.runtime.reflect.Accessor$GetterSetterReflection.optimize(Accessor.java:369)
        at com.sun.xml.bind.v2.runtime.property.ArrayProperty.<init>(ArrayProperty.java:65)
        at com.sun.xml.bind.v2.runtime.property.ArrayERProperty.<init>(ArrayERProperty.java:84)
        at com.sun.xml.bind.v2.runtime.property.ArrayElementProperty.<init>(ArrayElementProperty.java:96)
        at com.sun.xml.bind.v2.runtime.property.ArrayElementLeafProperty.<init>(ArrayElementLeafProperty.java:66)
        at sun.reflect.GeneratedConstructorAccessor172.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
        at com.sun.xml.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:124)
        at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.<init>(ClassBeanInfoImpl.java:179)
        at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:515)
        at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:330)
        at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1140)
        at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:154)
        at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:121)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:248)
        at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:235)
        at javax.xml.bind.ContextFinder.find(ContextFinder.java:432)
        at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:637)
        at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:584)
        at com.mycompany.global.er.util.FrameworkUtil.<clinit>(FrameworkUtil.java:119)

你检查 this bug 了吗? 似乎已在 jaxb 实现的 2.2.11 版中修复。

也许你可以尝试替换 jaxb JAR 文件,它们可能在你服务器的库中,除非你的 webapp 在它自己的库文件夹中覆盖它们。

<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.2.11</version>
</dependency>