Discussions

EJB programming & troubleshooting: java.lang.ClassCastException while lookup EJB 2.0

  1. Hi, 
       I practice to code EJB 2.0 but the error show on the context.lookup("JNDI").
    I code the below for Java standalone to call EJB it work fine    Hashtable jndiContextProp = new Hashtable(); jndiContextProp.put(Context.PROVIDER_URL, "ln5362-288:50004"); jndiContextProp.put(Context.SECURITY_PRINCIPAL, "Administrator"); jndiContextProp.put(Context.SECURITY_CREDENTIALS, "admin123"); jndiContextProp.put(Context.INITIAL_CONTEXT_FACTORY, "com.sap.engine.services.jndi.InitialContextFactoryImpl"); try { InitialContext ctx=new InitialContext(jndiContextProp); sayHelloHome home=(sayHelloHome) ctx.lookup("HelloWorldBeanJNDI"); sayHelloRemote remote=home.create(); System.out.println(remote.sayHello("Test")); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); }
    but i code the below on servlet to call EJB the error occur.
    try { InitialContext ctx=new InitialContext(); sayHelloLocalHome home=(sayHelloLocalHome) ctx.lookup("localejbs/HelloWorldBeanJNDI"); sayHelloLocal remote=home.create(); System.out.println(remote.sayHello("Test"));
    } catch (Exception e) { // TODO: handle exception e.printStackTrace(out); }
    The error state that java.lang.ClassCastException : incompatible with interface  on 'sayHelloLocalHome home=(sayHelloLocalHome) ctx.lookup("localejbs/HelloWorldBeanJNDI");'

    Below is the error trace : java.lang.ClassCastException: class

    $Proxy616:sap.com/HelloWorld_EAR@com.sap.engine.boot.loader.ResourceMultiParentClassLoader@1bc3150@alive incompatible with interface ejb.sayHelloLocalHome:sap.com/Servlet_Exc_EAR@com.sap.engine.boot.loader.ResourceMultiParentClassLoader@7101cd@alive at sap.trianing.Servlet_Exc_WEB.doGet(Servlet_Exc_WEB.java:43) at javax.servlet.http.HttpServlet.service(HttpServlet.java:707) 

     

    Pleased help.

  2. Hashtable jndiContextProp = new Hashtable(); jndiContextProp.put(Context.PROVIDER_URL, "ln5362-288:50004"); jndiContextProp.put(Context.SECURITY_PRINCIPAL, "Administrator"); jndiContextProp.put(Context.SECURITY_CREDENTIALS, "admin123"); jndiContextProp.put(Context.INITIAL_CONTEXT_FACTORY, "com.sap.engine.services.jndi.InitialContextFactoryImpl");

    try {

    InitialContext ctx=new InitialContext(jndiContextProp);

    sayHelloHome home=(sayHelloHome) ctx.lookup("HelloWorldBeanJNDI"); sayHelloRemote remote=home.create(); System.out.println(remote.sayHello("Test"));

    } catch (Exception e) { // TODO: handle exception e.printStackTrace(); }

  3. try {

    InitialContext ctx=new InitialContext();

    sayHelloLocalHome home=(sayHelloLocalHome) ctx.lookup("localejbs/HelloWorldBeanJNDI");

    sayHelloLocal remote=home.create();

    System.out.println(remote.sayHello("Test"));
    } catch (Exception e) { // TODO: handle exception e.printStackTrace(out); }

    The error Show on 

    sayHelloLocalHome home=(sayHelloLocalHome) ctx.lookup("localejbs/HelloWorldBeanJNDI");

     

     

  4. Solved[ Go to top ]

    Hi, 

       I can fix it with below code.

    try { InitialContext ctx=new InitialContext(); Object obj= ctx.lookup("localejbs/sap.com/HelloMan_EAR/HelloManBean"); ClassLoader homeCl = obj.getClass().getClassLoader();   Class<?> localHome = homeCl.loadClass(HelloManBeanLocalHome.class.getCanonicalName()); java.lang.reflect.Method method = localHome.getMethod("create"); Object homeObj = (Object)method.invoke(obj); 
    ClassLoader localCl=homeObj.getClass().getClassLoader(); Class<?> local = localCl.loadClass(HelloManBeanLocal.class.getCanonicalName()); Class  args[] = {String.class}; java.lang.reflect.Method localMethod=local.getMethod("helloMan",args);   Object params[]={"Thongie"}; Object localObj = (Object)localMethod.invoke(homeObj,params);   lookupText=(String)localObj; } catch (Exception e) { // TODO: handle exception e.printStackTrace(out); }

  5. I would suggest to always do a lookup from the servlet's environment like this:

    initialContext.lookup("java:comp/env/Home Interface JNDI name");

    What I am thinking should have been happened in your case is that it would have tried to Look up the Home stub but it didnt find and returned null object and then you are trying to map null to Home Interface resulted in classcastexception.

  6. I will follow[ Go to top ]

    I will follow your suggestions. This is so important for my future projects. - Michael Courouleau