Re: ClassCastException after retrieving an Entry

Gregg Wonderly <[email protected]> Wed, 3 Jan 2007 15:37:01 -0600
Newsgroups gmane.comp.java.sun.javaspaces
Message-ID <[email protected]>
Christian Hager wrote:
> java.lang.ClassCastException: org.ac.computegrid.jobs.fibfact.FactorialTask
>        at org.ac.computegrid.worker.GenericWorker.loop(GenericWorker.java:117)
>        at org.ac.mvc.ExtThread.run(ExtThread.java:399)
>        at java.lang.Thread.run(Unknown Source)
>
> The FactorialTask which I get from the Space is extends TaskEntry and
> therefore should be possible to be casted to TaskEntry.

Class equality includes the notion of ClassLoader equality too.  In most cases
when you see this in a Jini application, it means that the instance of the
class, org.ac.computegrid.jobs.fibfact.FactorialTask, that your code has
resolved to start running, is loaded with a different class loader than the
instance in the object you are casting to that class.

In Jini 2.0 and later, this can happen quite easily with a misconfigured
PREFERRED.LIST.  Any interface or class that is API data, should not be
preferred because you want to load the version that is visible in the receiving
clients/servers classpath so that it can "cast to" or otherwise use that class.

If you are not using a PREFERRED.LIST, or it's contents is not your problem,
then you'll need to check to see which classloaders are resolving which classes
to know how to solve the problem.

You can use reflection to traverse the class tree of the Entry, and print out
the value of getClassLoader() on each class to see where they are resolved.
Also print out the classloader of FactorialTask as it is referenced in your
software.

If you do

        FactorialTask tsk = (FactorialTask)entry;

then just use

        System.out.println("My FactorialTask Loader: "+
                FactorialTask.getClass().getClassLoader() );

so that you are using the same Class reference to find the loader.

Gregg Wonderly

===========================================================================
To unsubscribe, send email to [email protected] and include in the body
of the message "signoff JAVASPACES-USERS".  For general help, send email to
[email protected] and include in the body of the message "help".

To view past JAVASPACES-USERS postings, please see:
http://archives.java.sun.com/archives/javaspaces-users.html