10 Replies Latest reply on Apr 7, 2008 8:59 AM by Jeroen Wenting

    ClassCastExeption while invoking a Bean

    Alexander Tarabrin Newbie

      hi,

      evaluating a simpliest ejb3 I get the following exception


      java.lang.ClassCastException
      com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:229)
      javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:137)
      mypackage.MyClientClass.testBeanInvocation(MyClientClass.java:50)
      sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      java.lang.reflect.Method.invoke(Method.java:597)
      org.springframework.web.servlet.mvc.multiaction.MultiActionController.invokeNamedMethod(MultiActionController.java:351)
      org.springframework.web.servlet.mvc.multiaction.MultiActionController.handleRequestInternal(MultiActionController.java:305)
      org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:128)
      org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:44)
      org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:684)
      org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:625)
      org.springframework.web.servlet.FrameworkServlet.serviceWrapper(FrameworkServlet.java:386)
      org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:346)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
      org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)

      root cause

      java.lang.ClassCastException: org.jnp.interfaces.NamingContext cannot be cast to org.omg.CORBA.Object
      com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:212)
      javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:137)
      mypackage.MyClientClass.testBeanInvocation(MyClientClass.java:50)
      sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      java.lang.reflect.Method.invoke(Method.java:597)
      org.springframework.web.servlet.mvc.multiaction.MultiActionController.invokeNamedMethod(MultiActionController.java:351)
      org.springframework.web.servlet.mvc.multiaction.MultiActionController.handleRequestInternal(MultiActionController.java:305)
      org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:128)
      org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:44)
      org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:684)
      org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:625)
      org.springframework.web.servlet.FrameworkServlet.serviceWrapper(FrameworkServlet.java:386)
      org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:346)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
      org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)



      the executing code:

      Object ref = ctx.lookup("MyBean");
      Object narrow = PortableRemoteObject.narrow( ref, MyRemote.class); //exception comes here
      
      


      The bean to be invoked is a stateful session bean, deployed in the /server/default/deploy as a *.jar.

      Does anybody have an idea?

      Environment:
      Jboss 4.2.0 RC2
      Java 6
      Windows XP

      Regards,

      Ulmer

        • 1. Re: ClassCastExeption while invoking a Bean
          Carlo de Wolf Master

          You're trying to narrow a directory context. You don't need to narrow an EJB3 and you should lookup the remote instead.

          Please try:

          MyRemote bean = (MyRemote) ctx.lookup("MyBean/remote");


          • 2. Re: ClassCastExeption while invoking a Bean
            Alexander Tarabrin Newbie

            mean while I putted the jar with the ejb and the client war into an ear package.

            the jar file was completed with the /META-INF/jboss.xml:

            <?xml version="1.0" encoding="UTF-8"?>
            <jboss>
             <enterprise-beans>
             <session>
             <ejb-name>MyBean</ejb-name>
             <jndi-name>MyBean</jndi-name>
             </session>
             </enterprise-beans>
            </jboss>
            


            wolfc, trying your code I get the following (btw, using the narrow()-method makes the same):

            2007-05-15 14:31:26,953 ERROR [STDERR] javax.naming.NotContextException: jboss.j2ee:ear=testapp.ear,jar=mybean.jar,name=MyBean,service=EJB3:a14yg-w8mdj3-f1qcfl91-1-f1qcgfzd-9 is not a Context
            2007-05-15 14:31:26,953 ERROR [STDERR] at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:692)
            2007-05-15 14:31:26,953 ERROR [STDERR] at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:587)
            2007-05-15 14:31:26,953 ERROR [STDERR] at javax.naming.InitialContext.lookup(InitialContext.java:392)
            2007-05-15 14:31:26,953 ERROR [STDERR] at mypackage.MyTestForm.getTestList(G4SchnittstelleTestForm.java:53)
            2007-05-15 14:31:26,953 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            2007-05-15 14:31:26,953 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
            2007-05-15 14:31:26,953 ERROR [STDERR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
            2007-05-15 14:31:26,953 ERROR [STDERR] at java.lang.reflect.Method.invoke(Method.java:597)


            • 3. Re: ClassCastExeption while invoking a Bean
              Carlo de Wolf Master

              With that deployment descriptor it's:

              MyRemote bean = (MyRemote) ctx.lookup("MyBean");


              • 4. Re: ClassCastExeption while invoking a Bean
                Peter Gwiazda Newbie

                Open JNDIView (at http://localhost:8080/jmx-console/ by default) use the "list" method and look for Your beans in "Global JNDI Namespace" to find out what are the JNDI Names.

                I had a similar problem.

                • 5. Re: ClassCastExeption while invoking a Bean
                  Patricio Villalobos Newbie

                  i got the same exception, any tips?
                  none of the tips posted here works....

                  i have the same descriptors.. same scenario..
                  the 1st code was auto generated by netbeans 6

                  java.lang.ClassCastException
                   com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:229)
                   javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:137)
                   cl.imagicair.ldap.DatosLdap.lookupLdapInfo2Bean(DatosLdap.java:99)
                   cl.imagicair.ldap.DatosLdap.processRequest(DatosLdap.java:35)
                   cl.imagicair.ldap.DatosLdap.doGet(DatosLdap.java:68)
                   javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
                   javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
                   org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
                  
                  causa ra�z
                  
                  java.lang.ClassCastException: $Proxy141 cannot be cast to org.omg.CORBA.Object
                   com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:212)
                   javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:137)
                   cl.imagicair.ldap.DatosLdap.lookupLdapInfo2Bean(DatosLdap.java:99)
                   cl.imagicair.ldap.DatosLdap.processRequest(DatosLdap.java:35)
                   cl.imagicair.ldap.DatosLdap.doGet(DatosLdap.java:68)
                   javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
                   javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
                   org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
                  


                  • 6. Re: ClassCastExeption while invoking a Bean
                    Volodymyr Mykhalchuk Newbie

                    Hi!

                    I have the same error however I try to run EJB2.1 beans that were running well on JBoss 4.0.0.CR2 with JBoss 4.2.2.GA.

                    Should I migrate my EJB2.1 beans (and XML descriptors)? I have read that it is possible to use EJB2.1 and EJB3 together.

                    Thanks,
                    Volodymyr

                    • 7. Re: ClassCastExeption while invoking a Bean
                      Pavol Bernhauser Newbie

                      Use casting and narrowing to remote home interface.

                      Pavol

                      • 8. Re: ClassCastExeption while invoking a Bean
                        Atul Agarwal Newbie

                        Dear Sir,

                        I am new to ejb, my problem is when i run my client program i get this error.
                        Anyone suggest me what i am doing wrong...........

                        This is my Exception which is generated during the client program execute.......

                        Exception in thread "main" 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 Client.main(Client.java:27)
                        Caused by: java.lang.ClassCastException: $Proxy0 cannot be cast to org.omg.CORBA.Object
                        at com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:212)
                        ... 2 more

                        Could anyone tell me what I’m doing wrong? Thanks a lot. Looking forward to hearing from you.

                        These are my code..........

                        ********remote interface*******
                        package My;

                        import java.rmi.*;

                        import javax.ejb.*;

                        public interface DemoInter extends EJBObject
                        {
                        public int add() throws RemoteException;
                        }

                        ************home interface***********
                        package My;

                        import javax.ejb.*;
                        import java.rmi.*;



                        public interface DemoHome extends EJBHome
                        {
                        public DemoInter create(int a, int b) throws CreateException,RemoteException;
                        }
                        *************bean class*************

                        package My;

                        import javax.ejb.*;
                        import java.rmi.*;

                        public class DemoBean implements SessionBean
                        {
                        int a,b;
                        private SessionContext context;
                        public void setSessionContext(SessionContext ctx){this.context=ctx}

                        public void ejbCreate( int a,int b)
                        {
                        this.a=a;
                        this.b=b;
                        System.out.println("ejb Created");
                        }

                        public void ejbActivate(){System.out.println("ejbActivate()");}
                        public void ejbPassivate(){System.out.println("ejbPassivate()");}
                        public void ejbRemove(){System.out.println("distroyed");}
                        public int add() throws RemoteException
                        {
                        return(a+b);
                        }
                        }



                        ***********my client**************




                        import java.util.*;
                        import javax.rmi.PortableRemoteObject;
                        import javax.naming.*;
                        import javax.rmi.*;
                        import My.*;


                        public class Client {

                        /**
                        * @param args
                        */
                        public static void main(String[] args) {
                        // TODO Auto-generated method stub
                        try {

                        Properties properties=new Properties();
                        properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.RemoteInitialContextFactory");
                        properties.put("java.naming.provider.url","ejbd://localhost:4201");

                        Context ctx=new InitialContext(properties);
                        System.out.println("ic = " + ctx);

                        //MyTimeLocal myTimeLocal = (MyTimeLocal)context.lookup("java:comp/env/ejb/MyTimeBean");
                        // My.DemoInter remoteObj =(My.DemoInter)ctx.lookup("ejb/mgmt/MEJBRemoteHome");
                        Object object=ctx.lookup("ejb/mgmt/MEJBRemoteHome");
                        // System.out.println("hello" +object);
                        My.DemoHome ejbHome =(My.DemoHome)PortableRemoteObject.narrow(object,My.DemoHome.class);
                        System.out.println("hello" +ejbHome);
                        My.DemoInter obj1=ejbHome.create(4,5);
                        System.out.println(obj1.add());





                        } catch (Exception e) {
                        System.out.println(e);
                        // TODO: handle exception
                        }



                        }

                        }


                        this is log file..........

                        13:02:24,839 INFO [SupportedModesServiceImpl] Portlet mode 'edit' not found for portletId: '/plugin.Deployment!227983155|0'
                        13:02:30,987 INFO [SupportedModesServiceImpl] Portlet mode 'edit' not found for portletId: '/plugin.Deployment!227983155|0'
                        13:02:34,611 INFO [SupportedModesServiceImpl] Portlet mode 'edit' not found for portletId: '/plugin.Deployment!227983155|0'
                        13:02:35,732 INFO [SupportedModesServiceImpl] Portlet mode 'edit' not found for portletId: '/plugin.Deployment!227983155|0'
                        13:02:40,045 INFO [config] Configuring Service(id=Default Stateless Container, type=Container, provider-id=Default Stateless Container)
                        13:02:40,045 INFO [config] Configuring Service(id=Default Stateful Container, type=Container, provider-id=Default Stateful Container)
                        13:02:40,045 INFO [config] Configuring Service(id=Default BMP Container, type=Container, provider-id=Default BMP Container)
                        13:02:40,046 INFO [config] Configuring Service(id=Default CMP Container, type=Container, provider-id=Default CMP Container)
                        13:02:40,046 INFO [config] Configuring app: default/ejbsample1.jar/1207553559310/jar
                        13:02:40,078 INFO [OpenEJB] Auto-deploying ejb DemoEJB: EjbDeployment(deployment-id=ejbsample1.jar/DemoEJB)
                        13:02:40,079 INFO [config] Loaded Module: default/ejbsample1.jar/1207553559310/jar
                        13:02:40,976 INFO [startup] Assembling app: /home/atul/santemp/geronimo-tomcat6-javaee5-2.1/var/temp/geronimo-deployer17419.tmpdir/ejbsample1.jar
                        13:02:40,985 INFO [startup] Jndi(name=DemoEJBRemoteHome) --> Ejb(deployment-id=ejbsample1.jar/DemoEJB)
                        13:02:40,985 INFO [startup] Created Ejb(deployment-id=ejbsample1.jar/DemoEJB, ejb-name=DemoEJB, container=Default Stateless Container)
                        13:02:40,985 INFO [startup] Deployed Application(path=/home/atul/santemp/geronimo-tomcat6-javaee5-2.1/var/temp/geronimo-deployer17419.tmpdir/ejbsample1.jar)
                        13:02:41,083 INFO [SupportedModesServiceImpl] Portlet mode 'edit' not found for portletId: '/plugin.Deployment!227983155|0'

                        • 9. Re: ClassCastExeption while invoking a Bean
                          jaikiran pai Master

                          This part looks odd to me:


                          Properties properties=new Properties();
                          properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.RemoteInitialContextFactory");
                          properties.put("java.naming.provider.url","ejbd://localhost:4201");


                          This is what i normally use, when accessing a EJB from a standalone client:
                          Properties props = new Properties();
                           props.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
                           props.put(Context.PROVIDER_URL,"jnp://localhost:1099");
                           props.put(Context.URL_PKG_PREFIXES,"org.jboss.naming.jnp.interfaces");


                          replace localhost with appropriate servername/IP depending on where your server is located. Also, access the jndiview as mentioned at http://jboss.org/wiki/Wiki.jsp?page=DisplayTheJDNITreeWithTheJMXConsole and post the contents of the tree here.

                          While posting the logs or code or xml content, remember to wrap it in a code block using the Code button in the message editor window and please hit the Preview button to make sure your post is correctly formatted



                          • 10. Re: ClassCastExeption while invoking a Bean
                            Jeroen Wenting Newbie

                            He's not using JBoss (or is but has gotten his jndi parameters from something completely different).
                            He's not using EJB3, so he's asking his question in the wrong forum.
                            He's hijacking a dead thread for an unrelated message.

                            And finally he's trying to narrow his EJB stub to the home interface instead of the remote interface.