ClassCastFound when narrow the home interface

Discussions

EJB programming & troubleshooting: ClassCastFound when narrow the home interface

  1. ClassCastFound when narrow the home interface (4 messages)

    I encountered a strange ClassCastFound exception. Here's the snippet of a simple servlet code:

    =====================================================
    public String sayHello()
            throws NamingException,
                   RemoteException,
                   CreateException {

        Context context = new InitialContext();
        Object obj = context.lookup(
                          "java:comp/env/ejb/HelloSession");

        HelloSessionHome home = (HelloSessionHome)
                                PortableRemoteObject.narrow(
                                    obj,
                                    HelloSessionHome.class);

        return home.create().sayHello();
    }
    =====================================================

    HelloSession is an simple Stateless Session bean.

        EJB deployed at: Bea weblogic 7
    Servlet deployed at: Resin 2.1.2 or tomcat 4
               Webapp 1: hello1.war
               Webapp 2: hello2.war

    Both webapp 1&2 contains the same servlet which visits the HelloSession EJB. i.e., the following URLs are mapped to the 2 webapps respectively and show the same result in the browser:

        http://localhost:8080/hello1/helloservlet
        http://localhost:8080/hello2/helloservlet

    The problem is: the first servlet is OK, and the second servlet throws a ClassCastException:

    java.lang.ClassCastException
    at com.sun.corba.se.internal.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:296)
    at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:137)
    at com.alibaba.hello.web.HelloServlet.sayHello(HelloServlet.java:57)
    at com.alibaba.hello.web.HelloServlet.doGet(HelloServlet.java:38)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:126)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:103)
    at com.caucho.server.http.FilterChainServlet.doFilter(FilterChainServlet.java:96)
    at com.caucho.server.http.Invocation.service(Invocation.java:311)
    at com.caucho.server.http.CacheInvocation.service(CacheInvocation.java:135)
    at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:221)
    at com.caucho.server.http.HttpRequest.handleConnection(HttpRequest.java:163)
    at com.caucho.server.TcpConnection.run(TcpConnection.java:137)
    at java.lang.Thread.run(Thread.java:484)

    Anyone can help? tks in advance.
  2. But where is deploed the ejb ?

    Are you sure that the second webapp have all necessary classes in its classpath ?

    The interposition classes (the client side of ejb) are present in class-path of webapp2 ?

    Check the manifest.mf in the META-INF directory of webapp1 and webapp2 and bu sure to include the jar file of EJB in all webapps.
  3. I agree: at first glance it looks very much like a Manifest file issue.
  4. Both wars contain ALL neccessary files and ejb-client jar in their directory WEB-INF/lib.

    In fact, hello2.war is a copy of hello1.war. Each war works fine if we visit this war at first. for example,

      1. start resin or tomcat server, whose directory "webapps" contains hello1.war and hello2.war

      2. visit http://localhost:8080/hello1/helloservlet, works fine

      3. visit http://localhost:8080/hello2/helloservlet, which throws ClassCastException!

    If we restart the servlet engine, and visit hello2 before hello1, then hello2 is ok, and hello1 failed.

    I have tried to package the 2 wars and the ejb-jar into an ear file, and deploy it to weblogic server, then everything is ok.

    Any idea?
  5. Kong,

    could u tell me where exactly this ejb access code is called from, init or service etc?
    If it is from init then there might me a servlet/ejb context issue and it would work fine in service........