7 Replies Latest reply on Jan 13, 2007 10:42 PM by kingcu

    Please help with EJB3 ClassCastException.

    kingcu

      Hi Friends,

      I am writing my first ejb 3 project that has a simple stateless session bean, which is accessed by my struts action class. Here is how the bean class looks like:

      @Stateless
       @Local ({BeanLocal.class})
       @LocalBinding (jndiBinding="myapp/BeanLocal")
       @Remote ({BeanRemote.class})
       @RemoteBinding (jndiBinding="myapp/BeanRemote")
       public class Bean implements BeanLocal, BeanRemote {
       ...
       }
      



      However, I got ClassCastException in my action class when trying to cast the bean to its local/remote interface type:

      BeanLocal bl = (BeanLocal) jndiContext.lookup("myapp/BeanLocal");


      or for remote interface:

      Object ref = jndiContext.lookup("myapp/BeanRemote");
       BeanRemote br = (BeanRemote) PortableRemoteObject.narrow(ref, BeanRemote.class);
      



      I am using JBoss Eclipse IDE 1.6 and running JBoss AS 4.0.5. The EJB 3 code is packaged into ejb-jar.jar and the web app is in its war file, then both the jar and the war are packed into an ear file.

      Can someone please help me figure out what's wrong here?

      Thanks,
      Tong

        • 1. Re: Please help with EJB3 ClassCastException.
          peterj

          I think you want to use a different JNDI name:

          BeanLocal bl = (BeanLocal) jndiContext.lookup("myapp/BeanLocal/local");

          and

          Object ref = jndiContext.lookup("myapp/BeanRemote/remote");

          • 2. Re: Please help with EJB3 ClassCastException.
            peterj

            Oops, glanced right over the annotations. (It's Friday afternoon, I must be tired.) Check with JNDIView to see what names you actually got.

            • 3. Re: Please help with EJB3 ClassCastException.
              kingcu

              I can see the right jndi names in jmx-console. And in fact, I was using the default jndi names initially, but did not work; then I changed to custom jndi.

              • 4. Re: Please help with EJB3 ClassCastException.
                mmarcom

                can you pls post her eyour exception?
                i remember having same problem and i fixed it by changing a parameter in ejb3-deployable ....

                regards
                marco

                • 5. Re: Please help with EJB3 ClassCastException.
                  knuffelbert

                  I had the same problem when I started a couple days ago, maybe the solution is the same...

                  In my jboss.xml file, the specified jndi name was incorrect.

                  • 6. Re: Please help with EJB3 ClassCastException.
                    kingcu

                    Here is the exception:

                    22:33:03,944 ERROR [[action]] Servlet.service() for servlet action threw exception
                    java.lang.ClassCastException
                     at com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:229)
                     at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:137)
                     at com.kfi.was.actions.SponsorAction.list(SponsorAction.java:51)
                     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                     at java.lang.reflect.Method.invoke(Method.java:585)
                     at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:266)
                     at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:167)
                     at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:413)
                     at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:225)
                     at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1858)
                     at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:446)
                     at javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
                     at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
                     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
                     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                     at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
                     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
                     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
                     at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175)
                     at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
                     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
                     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
                     at org.jboss.web.tomcat.tc5.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:156)
                     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
                     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
                     at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
                     at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
                     at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
                     at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
                     at java.lang.Thread.run(Thread.java:595)
                    Caused by: java.lang.ClassCastException: $Proxy79
                     at com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:212)
                     ... 33 more
                    


                    • 7. Re: Please help with EJB3 ClassCastException.
                      kingcu

                      As for jboss.xml, I don't have it in my ear; I think if I use annotation, I can avoid xml configuration.

                      Also, since JBoss 4.0.5 doesn't support J2EE 5 yet, my application ear is still J2EE 1.4; not sure if this will cause any problem.

                      Here is what my application.xml looks like:

                      <?xml version="1.0" encoding="UTF-8"?>
                      <application version="1.4"
                       xmlns="http://java.sun.com/xml/ns/j2ee"
                       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                       xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
                       http://java.sun.com/xml/ns/j2ee/application_1_4.xsd">
                      
                       <module>
                       <ejb>ejb3beans.jar</ejb>
                       </module>
                      
                       <module>
                       <web>
                       <web-uri>was.war</web-uri>
                       <context-root>was</context-root>
                       </web>
                       </module>
                      
                      </application>