java.lang.ClassCastException: Unable to load class

java.lang.ClassCastException: Unable to load class: ._Stub can be occurred when classloader can not find the EJB client side binding. This also explains the general java explanation about the ClassCastException.

Explanation:
This exception occurs when the application program attempts to narrow to the EJB's home class and the classloaders cannot find the EJB's client side bindings. If occurred ClassCastException is not related with EJB, then see General Explanation of ClassCastException.

Possible causes:
  1. The files, *_Stub.class and _Tie.class, are not in the EJB .jar file
  2. Classloader could not find the classes
Solution:
Look at the EJB .jar file located in the .ear and verify the class contains the EJB client side bindings. These are class files whose names end in _Stub and _Tie. If these files are not present, then use the

Application Assembly Tool to generate the binding classes. For more information, see article Generating deployment code for modules. If the binding classes are in the EJB .jar file, then you might have a classloader issue.

General Explanation of ClassCastException:

According to java documentation, this exception occurres when code tried to cast an object to a subclass of which, it is not an instance. For example, the following code generates a ClassCastException:
Object x = new Integer(0);
System.out.println((String)x);
Note: One point you should remember that EJB JNDI name would be ejb/ejbname in the case of EARs are different. At the other hand you need to use java:comp/env/ejb/ejbname.
Do you enjoy this article? please help spread the word. And if you don't, then please let us know why?