0

I'm getting an ClassCastException while calling an EJB in an Pojo over JNDI. I use Oracle Weblogic Server 10.3.6 (EJB 3.0).

My structure:

  • app.ear
    • lib
      • Interfaces.jar
        • MyBeanInterface.java
    • ejb.jar
      • MyBeanImpl.java
    • webapp.war
      • Client.java
      • WEB-INF
        • web.xml

My local Interface:

package mypackage;

@Local
public Interface MyBeanInterface {}

My EJB-Class:

package mypackage;

@Stateless(name = "MyBean")
public class MyBeanImpl implements MyBeanInterface {}

My Client (not an EJB):

MyBeanInterface bean = (MyBeanInterface) new InitialContext().lookup("java:comp/env/ejb/MyBean");

My web.xml

 <ejb-local-ref>
   <ejb-ref-name>ejb/MyBean</ejb-ref-name>
   <ejb-ref-type>Session</ejb-ref-type>
   <local>mypackage.MyBeanInterface</local>
</ejb-local-ref>

My Exception:

The Lookup itself works. I get a reference. But when I want to cast with (MyBeanInterface) I get the following error:

Cannot cast an instance of "class mypackage.MyBeanInterface_whjkp6_MyBeanImpl (loaded by instance of weblogic.utils.classloaders.GenericClassLoader(id=28136))" to an instance of "interface mypackage.MyBeanInterface(loaded by instance of weblogic.utils.classloaders.GenericClassLoader(id=28144))

What can I do?

Fireworx
  • 11
  • 5

1 Answers1

0

It seems the classes are loaded by different class loaders. Possible options are:

1) Ensure the classes are loaded by same class loader

2) Use reflection

3) Serialize and then deserialize

Refer:

1) cast across classloader?

2) https://community.oracle.com/thread/757133

3) ClassCastException because of classloaders?

Community
  • 1
  • 1
Kumar
  • 1,536
  • 2
  • 23
  • 33