I had a problem with my application. To resume the problem, I had to do migrate an application from jboss 4 to jboss 5.
During the war deployement, I had this error:
java.lang.LinkageError: loader constraint violation: when resolving field "DATETIME"
the class loader (instance of org/jboss/classloader/spi/base/BaseClassLoader) of the referring class,
javax/xml/datatype/DatatypeConstants, and the class loader (instance of <bootloader>)
for the field's resolved type, javax/xml/namespace/QName,
have different Class objects for that type
After many searchs, I found this error was here because I had several time the same class in differents packages. Once in a dependency package (from my pom.xml) and once provided by jboss.
So, to resolve this problem, I have give a scope "provided" for my dependency.
But I don't understand why this solution works. I thought it works to have several time the same class in an application. I know it's not a good thing, but with jboss 4, it's work.
Someone can explain me why it works with jboss 4 and not with jboss 5.
Thanks for your explanation :)
What you are seeing is the effect of an application server loading JBoss' libraries and EAR libraries in separate classloaders
You can think of an EAR's class loader hierarchy similar (but not necessarily) to :
Bootstrap ClassLoader -> System Class Loader -> JBoss System Class Loader -> Ear Class Loader -> War Class Loader.
Where war class loader's parent is the ear class loader and so forth.
Now if Bootstrap ClasssLoader has a jar A loaded and the ear is also being deployed with jar A, Bootstrap Class Lodaer and Ear Class Loader will have the same class created twice in separate class loaders.
I would assume (not 100% sure of this) that JBoss 4 didnt come bundled with javax/xml/namespace/QName. If that is true JBoss 5 is more then likely a different, upgraded, version of Java (4 -> 5 or 5 -> 6). As a result (with the new JBoss 5), when you try to pass javax/xml/namespace/QName into one of your classes, it is expecting the class from the ear. However you are giving it the QName class from the Bootstrap classloader because of the classloader preferences (parent first and so forth).
Since the class types are equal but the class instances are not equal you get a LinkageError
Edit:
Just two address the two comments -
The class loading behavior as jtahlborn noted is deffinitely different. In normal applications, the system classes like QName will be consistently looked for in the bootstrap classloader. In your error it looks as if javax/xml/datatype/DatatypeConstants is being loaded in org/jboss/classloader/spi/base/BaseClassLoader. Lets assume thats the EAR classloader (or WAR). A quick google shows that is part of the xml-apis' family and possibly jaxp-api.
So somewhere in your code (or another libraries code located in the EAR's class loader) needed DatatypeConstants - which forced the lookup of the class in the EAR's classloader. The creation of the QName object though loaded the class from the bootstrap classloader (instead of the EAR). This would happen if the QName class has already been initialized by the system, which it probably has.
This as you can imagine isn't suppose to happen. It actually looks like you have parent-last. Because when loading a class off the JBoss class loading mechanism, had parent-first been enabled, the initial DatatypeConstants would have returned the parent's (bootstrap) DatatypeConstants and not the childs. So as jtahlborn noted you would want the children's classloader here to be ignored.
As far as the resolution goes, unless you need the dependencies for a specific reason (like a slightly newer version is better then the current) I would delegate to the jboss's implementation. If thats not the case, you can take a look at the class-loading java2ClassLoadingCompliance
element that the jboss configuration has.
-verbose:class
in VM args will give how the Class is getting loaded.
If there is duplicate you can remove the conflicting jar/jars.
Thank you for the explanation - it was very helpful.
I learned that this issue is related to a JBoss bug which was fixed in a 5.0.0 release. But even though I am running an older version of JBoss, I still got this error.
Anyway, I did extensive research and run maven dependency tree a few times to see where the duplicate definitions are coming from. I was finally able to resolve this error by adding two dependencies - with scope set to provided - to my main pom:
(sun.jaxb-impl 2.1 , javax.jaxb-api 2.2 ) I hope this information helps someone.