ClassCastFound when narrow the home interface


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,
                   CreateException {

        Context context = new InitialContext();
        Object obj = context.lookup(

        HelloSessionHome home = (HelloSessionHome)

        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:


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

    at javax.rmi.PortableRemoteObject.narrow(
    at javax.servlet.http.HttpServlet.service(
    at javax.servlet.http.HttpServlet.service(
    at com.caucho.server.http.FilterChainServlet.doFilter(
    at com.caucho.server.http.Invocation.service(
    at com.caucho.server.http.CacheInvocation.service(
    at com.caucho.server.http.HttpRequest.handleRequest(
    at com.caucho.server.http.HttpRequest.handleConnection(

    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 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........