File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Class Cast Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Class Cast Exception" Watch "Class Cast Exception" New topic

Class Cast Exception

vasuma patel

Joined: Mar 07, 2006
Posts: 15
I get a class cast exception on the same class. The exception message is
Caused by: java.lang.ClassCastException: com/A/B/SelectionsBean incompatible with com/A/B/SelectionsBean

This appears really wierd to me. I did some serach and came with this which is close to my problem but didn't quite understand the isuue.

Appreciate any inputs.

Thanks in advance

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
If you load the same class file through two class loaders, Java considers them to be different classes in memory. This might be the problem.

Java has a family of ClassLoaders that load the .class files from disk or jars or remote servers or wherever they are. There are a special loaders for starting up an application but by the time a simple application starts running your code it has put a class loader in place for you and all of your classes will be loaded by the same class loader. Your casting problem shouldn't come up.

But in more complex systems, especially in frameworks like the servlet or EJB containers, there may be more class loaders floating around. The whole setup is framework-specific ... one environment might be very different from another. In EJB land I once had to get the classloader that loaded "this" class in order to load another because of differences in the classpaths.

Here's a Classloader Paper from IBM ... I think it's one that I read a while ago and thought was pretty good. See if it helps.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
vasuma patel

Joined: Mar 07, 2006
Posts: 15
Thanks for the info. The error has gone after I restarted the App Server as suggested in the link.
I agree. Here's the link:
subject: Class Cast Exception
It's not a secret anymore!