4.2.5. Debug and Resolve ClassCastExceptions
- Search the application to find all JAR(s) that contain the class named by the
ClassNotFoundException. If there is a module defined for the class, find and remove the duplicate JAR(s) from the application's WAR or EAR.
- Find the JBoss module containing the class and explicitly define the dependency in the
MANIFEST.MFfile or in the
jboss-deployment-structure.xmlfile. For more information, see Class Loading and Subdeployments in the Class Loading and Modules chapter of the Development Guide for JBoss Enterprise Application Platform 6.
- If you are not able to resolve it using the steps above, you can often determine the cause of the problem by printing the class loader information to the log. For example, you see the following
ClassCastExceptionin the log:
java.lang.ClassCastException: com.example1.CustomClass1 cannot be cast to com.example2.CustomClass2
- In your code, print the class loader information for the classes named by the
ClassCastExceptionto the log, for example:
logger.info("Class loader for CustomClass1: " + com.example1.CustomClass1.getClass().getClassLoader().toString()); logger.info("Class loader for CustomClass2: " + com.example2.CustomClass2.getClass().getClassLoader().toString());
- The information in the log shows which modules are loading the classes and, based on your application, you need to remove or move the conflicting JAR(s).