ClassCastException

Hi there

I have been trying to pass an object between 2 web applications on the same tomcat server. I set crossContext for both apps to "true" in their context xml files.

I have created an interface "AuthProcessManager" and packaged it into a JAR file which is on the class path of both apps ( in their WEB-INF/classes/lib folders). The object being passed implements this interface.

The class is received at web application 2 ok. And if it is cast to an object, there is no error. But when I try to cast it to the name of the interface it actually implements, I get ClassCastException.

Here is the code:

ServletContext scont =null;

ServletConfig sconf =null;

AuthProcessManager mgr=null;

sconf= servlet.getServletConfig();

scont = sconf.getServletContext().getContext("/" + contextName);

Object obj = scont.getAttribute(Constants.AUTH_PROCESS_MANAGER);

log.debug(Constants.AUTH_PROCESS_MANAGER +" from " + contextName +" => classname:" + obj.getClass().getName());

mgr = (AuthProcessManager)obj;

The error occurs on the final line. The actual error message is:

ERROR [http-80-Processor23] ActionExceptionHandler.logException(

145) | java.lang.ClassCastException: $Proxy21 cannot be cast to com.bitwalker.authdownloads.service.AuthProcessManager

I do not really understand why I get $Proxy21 as my class name. The actual class name is AuthProcessManagerImpl.

I have also tried putting the JAR file into tomcats common lib folder.But the result doesn't change. I have been battling this for days.

Can anyone help me?

[1814 byte] By [justin_a] at [2007-11-27 5:19:24]
# 1
Did you check that both application uses EXACTLY the same version of JAR file where your Interface and Implementation class are packaged in?$ProxyXX is just right as your web application is receiving a proxy class from the other web application.
eadelarosaa at 2007-7-12 10:42:45 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Thanks for your reply. Yes I have confirmed that the libraries are the same.

I thouht that it may be because the object is instantiated by Spring, when it is passed to another webapp, which also uses Spring, the proxy is unusable.

But I have just tried passing the object, directly instantiated by new AuthProcessManager(), with no proxies at all, and I still get the same error.

This is driving me nuts.

justin_a at 2007-7-12 10:42:45 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Your problem here is basically about class loader that each web application is using.

If you are referencing or passing object from another web context, you need to make sure that class loaders used on both web context are the same. If you create an object using Spring, and the other web context uses its default class loader (not Spring), that will cause an unexpected behavior.

eadelarosaa at 2007-7-12 10:42:45 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Hmm. Well they both use the same classloader.

Could it be because the implementing class contains references to other classes in its original web application context? Though the interface(AuthProcessManager) does not contain such references, the implementing class (AuthProcessManagerImpl) does.

justin_a at 2007-7-12 10:42:45 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
But aren't those classes being referenced by implementing class present as well in the other web?However, if that's the case, it should throw a ClassNotFoundException, isn't it?
eadelarosaa at 2007-7-12 10:42:45 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

Yeah, I am just guessing now I suppose. The classes referenced by implementig class are all present in the original web application, but not in the second web app to whic the class is being passed.

Basically the second web app does soe processing and passes the results via the implementing class to the back end of the first web app.

justin_a at 2007-7-12 10:42:45 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...