ClassCastException: During Local EJB lookup with WAS 6.0


EJB programming & troubleshooting: ClassCastException: During Local EJB lookup with WAS 6.0

  1. Hi I got the following error during the lookup time of EJB Local interface, please help me to solve the problem. ( Websphere 6.0.1, Jsp, EJB2 ) ------- error ------ R java.lang.ClassCastException: test.EJSLocalStatelesscounterHome_8ada3073 at at javax.servlet.http.HttpServlet.service( at at at at ejb-jar.xml ------------ An EJB named counter Ejb Lccal Counter Counter test.CounterLocalHome test.CounterLocal test.CounterBean Stateless Container ejb/Counter Session test.CounterLocalHome test.CounterLocal counter.jar#Counter web.xml ------- counter index.jsp <!-- To refer local EJB's --> ejb/Counter Session test.CounterLocalHome test.CounterLocal counter.jar#Counter Client code ( JSP client) ------------------------ try { InitialContext context = new InitialContext(); out.println("Context created....."); CounterLocalHome home =(CounterLocalHome)context.lookup("java:comp/env/ejb/Counter"); CounterLocal remote=home.create(); out.println(" hit count....."+remote.hit()); out.println(" === finished ===== "); } catch (Exception e) { out.println(e); e.printStackTrace(); } %> ============================================================ I try to Print the lookup Class before going to cast. it print following output, out.print("Output="+context.lookup("java:comp/env/ejb/Counter").toString()); Output === test.EJSLocalStatelesscounterHome_8ada3073@181b8107(BeanId(__homeOfHomes#__homeOfHomes#__homeOfHomes, counter_jar#counter.jar#counter)) please help me to solve the problem....... Advance Thanks.............
  2. The problem is due to classloading, and incompatible classes at the server vs client (even if they are in the same jvm, may be they are loaded from different jars etc). The only solution to this problem is recompile and redeploy and ensure that there are no duplicates of this class present in any other jar. Anyway, as a temporary workaround, you can use reflection to achieve the same : YOUR CODE : CounterLocalHome home =(CounterLocalHome)context.lookup("java:comp/env/ejb/Counter"); CounterLocal remote=home.create(); USING REFLECTION : import java.lang.reflect.*; Object oHome = context.lookup("java:comp/env/ejb/Counter"); Method create = oHome.getMethod("create",null); Object remote = create.invoke(oHome, null); //Now try to cast remote to CounterLocal, if you recieve the same class cast exception, then do the same reflection way as mentioned above, to invoke the methods inside the remote object.
  3. Hi James If i use Reflection, its working fine.. Thanks lot.. regards Babu Jayaraman