Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Java in General and the fly likes ClassCastException because of class loaders Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "ClassCastException because of class loaders" Watch "ClassCastException because of class loaders" New topic

ClassCastException because of class loaders

gaurav abbi
Ranch Hand

Joined: Jan 05, 2007
Posts: 108
hi all,
i've a Http Server on which a web-app run using a binding port,this works fine,
but problem arises when same jars are present in WEB-INF/lib of web-app and also on my server.
Based on some logic web-app can call some internal services of my server, but in case i'm using any jar which is present both WEB-INF/lib and my server, i get the following exception

here xerces.jar was present in both the places.

i googled for this and what i got is this happens because some object would have been created using class loader of web-app(org.apache.jasper.servletJasperLoader) and in my internal functionality when i try to retrieve the object it has got parent class loader(ContextLoader)
there this issue will come.

but i went through the code, there nowhere any API from xerces.jar is used or any arguments of methods getting called have any object related to xerces.jar
the xerces functionality is used only in the my internal service which is getting called.

the issue gets resolved when the jar is removed from web-application, but i'm not sure if thats the only solution, because we can't put a restriction on the web-application library as its external.

sorry for this lengthy message, but i can't help it
please reply if any one has faced any such issue or can put some light on that.

thanks,<br />gaurav abbi
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
the issue gets resolved when the jar is removed from web-application

Unfortunately, I think that is the only solution. If two different class loaders are in use for the same library in one jvm, this is likely to happen. How about duplicating the "internal services" in your own application?

gaurav abbi
Ranch Hand

Joined: Jan 05, 2007
Posts: 108
can i go for a customized ClassLoader which can resolve this issue for me??
i've read somewhat about that, but not sure if in such kind of scenario, this is possible...
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
The solution would be to use fewer classloaders, not more, but that's not an option here.

The code doesn't need to reference any Xerces API directly. If it does any XML processing, then it probably defaults to using it. Can you configure your XML processing code to use a different parser instead, e.g. Crimson?
[ July 16, 2007: Message edited by: Ulf Dittmer ]
gaurav abbi
Ranch Hand

Joined: Jan 05, 2007
Posts: 108
hi Ulf,
we got a solution for this problem,
i'm doing something like this

this thing is working fine, but i've a doubt, the output of my service is a String using which i create a org.w3c.dom.Document type (resultDomDoc) object at 2
i get this object in the context of my parent class loader which i set at 1.
but after setting back the ContextClassLoader back to original loader if i try to refer resultDomDoc, i should get ClassCastException , but to my utter wonder its going fine and i'm able to retrieve results from the Document object successfully.
i'm doubtful something is wrong, but what i can't say...

[ July 17, 2007: Message edited by: gaurav abbi ]
I agree. Here's the link:
subject: ClassCastException because of class loaders
It's not a secret anymore!