ClassCastException in between equal classes Wildfly 10

大兔子大兔子 提交于 2019-12-01 11:30:40
Jan Piel

The answer from aribeiro is the right answer: A jar, packaged by the build-tool(Maven) is in the lib-directory of the war and conflicts with the wildfly-Classloader.

Additional,here is a generic solution for the following issue:

  • you've got a JBoss 7 AS or a Wildfly AS
  • You work with Eclipse JBoss Tools
  • you deploy an EAR or a WAR, lib-Folder is packed by build-tool like Maven
  • there occurs an error like this: examplepackage.ExcampleClass cannot be cast to examplepackage.ExcampleClass

Mostly it happens, when you leave the Java EE-Spec and use JBoss/Wildfly-specific modules, ironjacamar, jsf-impl and so on. Maven puts the required jars in the lib-directory. But the Jars are already in the module-directory of the AS. According to my experience, the ClassCastException occurs for this reason.

You must then remove the jar from the lib-directory, in maven by declaring the dependency as provided.

Tell the JBoss/Wildfly-Classloader, where to find the class. This can be done by:

  • Either add a dependency to the MANIFEST.MF of the project. Eclipse-JBoss-Tools loads the class from the Server-Enviroment, so you can remove the dependency from the pom. In your case:

    Dependencies: org.jboss.ironjacamar.jdbcadapters
    
  • Or you use the jboss-deployment-structure.xml, then you can load the class with Maven, but declare it as provided

    <dependencies>
        <module name="org.jboss.ironjacamar.jdbcadapters" />
    </dependencies>
    

See also https://docs.jboss.org/author/display/WFLY8/Class+Loading+in+WildFly

Clearly, as already stated, you're having a classloader issue due to a dependency clash.

To solve it, you should mark your wildfly-connector dependency as provided.

<dependency>
    <groupId>org.wildfly</groupId>
    <artifactId>wildfly-connector</artifactId>
    <version>10.0.0.Final</version>
    <scope>provided</scope>
</dependency>

WildFly already contains the JAR. If you go to the following folder you can find it:

<wildfly-10.0.0.Final-dir>\modules\system\layers\base\org\jboss\as\connector\main

There's no need for you to pack it with your application.

Nevertheless, as stated on WildFly 10's Implicit module dependencies for deployments, one needs to trigger the JCA sub-system explicitly. For that, one can make use of jboss-deployment-structure.xml with the following content:

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
    <deployment>
        <dependencies>
            <module name="org.jboss.ironjacamar.jdbcadapters" slot="main"/>            
        </dependencies>
    </deployment>
</jboss-deployment-structure>

Then, add your jboss-deployment-structure.xml to the WEB-INF folder of your WAR.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!