How to resolve java.lang.ClassCastException :
I'm using some third party jar file, there are plenty of Jar files as well, and I have the code written by someone else. While executing, I'm geting this error:
java.lang.ClassCastException : org.apache.axis.client.Service cannot be cast to org.apache.axis.client.Service
Can you suggest me what could开发者_JS百科 be the problem?
Thanks for the reply.
But before getting this error, I was getting:
java.lang.LinkageError : loader constraint violation: when resolving method "org.onvif.www.ver10.device.wsdl.DeviceBindingStub.(Ljava/net/URL;Ljavax/xml/rpc/Service;)V" the class loader (instance of org/apache/catalina/loader/WebappClassLoader) of the current class, com/abcd/efgh/ijkl/device/onvif/DeviceHandlerClient, and the class loader (instance of org/apache/catalina/loader/StandardClassLoader) for resolved class, org/onvif/www/ver10/device/wsdl/DeviceBindingStub, have different Class objects for the type javax/xml/rpc/Service used in the signature.
The problem has not resolved yet. I'm not getting where the version mismatch of files/jar
is happening from. I have included some Jars manually.
You likely have the Service class being sourced from two different class loaders. A classes identity is a combination of its name and the class loader that loaded the class.
As a contrived example, you could have two web apps deployed in something like Tomcat. They each have a copy of the same jar, and each loads their own instance of a class. Since they're in separate WARs, however, they each have different class loaders.
Next, lets say that you have something global, like a caching library or something. This caching module has its jar not within an individual WAR, but on the system class path for the container, that way it's visible to both WARs.
Web app 1 stores an instance of the class in to the cache, and then web app 2 tries to load that instance and cast it to its local version of the class. But since it's from the other web app, it has a different class loader than web app 2's. Thus when it's trying to cast the instance, it fails because it's not web app 2's version of that class.
So, basically, you have some kind of class loader confusion happening here. Make sure you have a clear understanding of what jars you're using, ensuring that there aren't any unnecessary duplications, and what context is loading what classes.
精彩评论