Search code examples
javawebsphereclassloaderwebsphere-7classcastexception

Externalising resource Adapter giving : java.lang.ClassCastException someclass incompatible with someclass


I am working on externalizing Resource adapter rars.

Earlier, the rar were packaged inside the /lib of war and everything worked good. Now to make the war light and also generic, I want to externalize resource adapter.

What I have done yet

Removed rars from war 
installed rar externally through WAS7.0 Admin console
configured J2C connection factories for each RAR 

I did a clean ,restart and I got some ClassNotFoundErrors.

Why were these errors there : Basically the rars use some jars that are present inside /lib. so earlier there was no problem but now when I externalized it, I started getting CNFE`s.

How I resolved: When we install a rar through WAS admin console , there is an option to provide classpath. I provided the jars that were causing issues on classpath there. And I could deploy and start my application

The problem:

When I login to my application. There is a line of code in one of the jars (that was causing issues and was added to classpath of resource adapter , note that currently this is present inside war and also on classpath of resource adapter), that is doing a type cast. Now on this statement

I get an exception

java.lang.ClassCastException: com.csc.fs.ra.SimpleMappedRecord incompatible with com.csc.fs.ra.SimpleMappedRecord

I dug up and found that a possible cause is multiple version of same jars. which is a case in my case. i have a version of jar inside war library and also on classpath of resource adapter.

I am kind of out of ideas here. what to do to resolve this kind of situation. please help

Regards


Solution

  • The RAR and the WAR got their own ClassLoader, even if you use the same version of the jar, each one of them loads the class separately and you get ClassCastException.

    Before when it was embedded it worked because the RAR was using the same ClassLoader.

    If the RAR are now separate I think you will have to put the jars in a shared library so it will be loaded by a single ClassLoader.