3 Replies Latest reply on Dec 2, 2002 6:52 PM by Doug Palmer

    java.lang.ClassCastException

    Shweta Garg Newbie

      I have deployed a bean and I m using a servlet client to access the bean.I m using the following code :

      LoginHome loginHome;
      Hashtable env = new Hashtable();
      env.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
      String url = "localhost:1099";
      env.put(Context.PROVIDER_URL, url);
      Context ctx = new InitialContext(env);

      Object objref = ctx.lookup("ejb/quiz/LoginBean");
      loginHome = (LoginHome)PortableRemoteObject.narrow(objref, LoginHome.class);


      I have put <jndi-name>ejb/quiz/LoginBean</jndi-name> tag in jboss.xml and then build the ear file.
      But, when I try to get access to bean using my servlet client I get the following exception raised at JBoss server :

      12:52:04,900 ERROR [STDERR] java.lang.ClassCastException
      12:52:04,900 ERROR [STDERR] at com.sun.corba.se.internal.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:293)
      12:52:04,900 ERROR [STDERR] at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:134)
      12:52:04,900 ERROR [STDERR] at quiz.login.CheckUserServlet.doGet(CheckUserServlet.java:80)
      12:52:04,900 ERROR [STDERR] at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
      12:52:04,900 ERROR [STDERR] at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
      12:52:04,900 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
      12:52:04,900 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
      12:52:04,900 ERROR [STDERR] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:260)
      12:52:04,900 ERROR [STDERR] at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      12:52:04,900 ERROR [STDERR] at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      12:52:04,910 ERROR [STDERR] at org.apache.catalina.core.ContainerBase.invokeContainerBase.java:995)
      12:52:04,910 ERROR [STDERR] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
      12:52:04,910 ERROR [STDERR] at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      12:52:04,910 ERROR [STDERR] at org.apache.catalina.valves.CertificatesValve.invoke(CertificatesValve.java:246)
      12:52:04,910 ERROR [STDERR] at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
      12:52:04,910 ERROR [STDERR] at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      12:52:04,910 ERROR [STDERR] at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      12:52:04,910 ERROR [STDERR] at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2396)
      12:52:04,910 ERROR [STDERR] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
      12:52:04,910 ERROR [STDERR] at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      12:52:04,910 ERROR [STDERR] at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
      12:52:05,021 ERROR [STDERR] at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
      12:52:05,031 ERROR [STDERR] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
      12:52:05,031 ERROR [STDERR] at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
      12:52:05,031 ERROR [STDERR] at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:469)
      12:52:05,031 ERROR [STDERR] at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
      12:52:05,031 ERROR [STDERR] at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      12:52:05,031 ERROR [STDERR] at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      12:52:05,031 ERROR [STDERR] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
      12:52:05,031 ERROR [STDERR] at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      12:52:05,031 ERROR [STDERR] at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      12:52:05,031 ERROR [STDERR] at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      12:52:05,031 ERROR [STDERR] at org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:1040)
      12:52:05,031 ERROR [STDERR] at org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1151)
      12:52:05,101 ERROR [STDERR] at java.lang.Thread.run(Thread.java:536)
      12:52:05,101 ERROR [STDERR] java.lang.NullPointerException
      12:52:05,101 ERROR [STDERR] at quiz.login.CheckUserServlet.doGet(CheckUserServlet.java:116)
      12:52:05,111 ERROR [STDERR] at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
      12:52:05,111 ERROR [STDERR] at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
      12:52:05,111 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
      12:52:05,111 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
      12:52:05,111 ERROR [STDERR] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:260)
      12:52:05,111 ERROR [STDERR] at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      12:52:05,111 ERROR [STDERR] at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      12:52:05,111 ERROR [STDERR] at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      12:52:05,111 ERROR [STDERR] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
      12:52:05,111 ERROR [STDERR] at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      12:52:05,111 ERROR [STDERR] at org.apache.catalina.valves.CertificatesValve.invoke(CertificatesValve.java:246)
      12:52:05,111 ERROR [STDERR] at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
      12:52:05,121 ERROR [STDERR] at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      12:52:05,121 ERROR [STDERR] at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      12:52:05,121 ERROR [STDERR] at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2396)
      12:52:05,121 ERROR [STDERR] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
      12:52:05,121 ERROR [STDERR] at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      12:52:05,121 ERROR [STDERR] at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)
      12:52:05,121 ERROR [STDERR] at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
      12:52:05,121 ERROR [STDERR] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
      12:52:05,121 ERROR [STDERR] at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
      12:52:05,121 ERROR [STDERR] at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:469)
      12:52:05,121 ERROR [STDERR] at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
      12:52:05,121 ERROR [STDERR] at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      12:52:05,121 ERROR [STDERR] at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      12:52:05,261 ERROR [STDERR] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
      12:52:05,261 ERROR [STDERR] at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
      12:52:05,271 ERROR [STDERR] at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
      12:52:05,271 ERROR [STDERR] at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
      12:52:05,271 ERROR [STDERR] at org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:1040)
      12:52:05,271 ERROR [STDERR] at org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1151)
      12:52:05,271 ERROR [STDERR] at java.lang.Thread.run(Thread.java:536)

      Please help me

      Thanx and regards
      Shweta

        • 1. Re: java.lang.ClassCastException
          huy nguyen Newbie

          Hi, i'm having the same problem. Some people have suggested to not use the PortableRemoteObject.narrow(), but i still get the same error.

          Have u found out how to correct this error ?
          I deployed a jar file for my bean and a war file for my webapplication. Do i need to put my bean and servlet file in a ear application to make it work ?

          any insight will be gratefull,

          in great despair!!!

          • 2. Re: java.lang.ClassCastException
            Doug Palmer Newbie

            I'm also having the same problem.

            I can see the deployed bean in the JNDI tree as:

            +- ejb (class: org.jnp.interfaces.NamingContext)
            | +- member (class: org.jnp.interfaces.NamingContext)
            | | +- UIDStream (proxy: $Proxy30 implements interface org.charvolant.callout.base.UIDStreamRemoteHome,interface javax.ejb.Handle)

            Using code of the form:

            public void setUp() throws Exception {
            Properties props;
            InitialContext ctx;
            Object h;

            props = new Properties();
            props.load(this.getClass().getResourceAsStream("jndi.properties"));
            ctx = new InitialContext(props);
            h = ctx.lookup(this.UIDSTREAMTEST);
            System.err.println("Expected class is " + UIDStreamRemoteHome.class);
            System.err.println("Stream home is " + h + " class " + h.getClass());
            this.home = (UIDStreamRemoteHome) PortableRemoteObject.narrow(h, UIDStreamRemoteHome.class); }

            Nets me the output:

            Expected class is interface org.charvolant.callout.base.UIDStreamRemoteHome
            Stream home is ejb/member/UIDStreamHome class class $Proxy0

            • 3. Re: java.lang.ClassCastException
              Doug Palmer Newbie

              To continue,

              I've tried the same code with JDK 1.3.1 and JDK 1.4.1 with the same results.

              What's wierd is that if I include a little extra code:

              public void setUp() throws Exception {
              Properties props;
              InitialContext ctx;
              Object h;
              Class[] classes;
              int i;

              props = new Properties();
              props.load(this.getClass().getResourceAsStream("jndi.properties"));
              ctx = new InitialContext(props);
              h = ctx.lookup(this.UIDSTREAMTEST);
              System.err.println("Expected class is " + UIDStreamRemoteHome.class);
              System.err.println("Stream home is " + h + " class " + h.getClass());
              classes = h.getClass().getInterfaces();
              System.err.println("Number of interfaces: " + classes.length);
              for (i = 0; i < classes.length; i++) {
              System.err.println("Interface=" + classes[ i ]);
              }
              this.home = (UIDStreamRemoteHome) PortableRemoteObject.narrow(h, UIDStreamRemoteHome.class);
              }

              I get the following output

              Expected class is interface org.charvolant.callout.base.UIDStreamRemoteHome
              Stream home is ejb/member/UIDStreamHome class class $Proxy0
              Number of interfaces: 2
              Interface=interface org.charvolant.callout.base.UIDStreamRemoteHome
              Interface=interface javax.ejb.Handle

              So $Proxy0 does claim to implement the interface, even if the cast throws an exception.

              What's even more interesting is that if I set my classpath to point to the deployed member.jar, instead of the directory tree with class files in it, I get the following exception:

              javax.naming.CommunicationException. Root exception is java.lang.ClassNotFoundException: org.charvolant.callout.base.UIDStreamRemoteHome (no security manager: RMI class loader disabled)
              at sun.rmi.server.LoaderHandler.loadProxyClass(LoaderHandler.java:521)
              at java.rmi.server.RMIClassLoader$2.loadProxyClass(RMIClassLoader.java:639)
              at java.rmi.server.RMIClassLoader.loadProxyClass(RMIClassLoader.java:309)
              at sun.rmi.server.MarshalInputStream.resolveProxyClass(MarshalInputStream.java:241)
              at java.io.ObjectInputStream.readProxyDesc(ObjectInputStream.java:1469)
              at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1432)
              at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1626)
              at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1274)
              at java.io.ObjectInputStream.readObject(ObjectInputStream.java:324)
              at java.rmi.MarshalledObject.get(MarshalledObject.java:135)
              at org.jnp.interfaces.MarshalledValuePair.get(MarshalledValuePair.java:30)
              at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:483)
              at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:443)
              at javax.naming.InitialContext.lookup(InitialContext.java:347)
              at org.charvolant.callout.base.test.UIDStreamTest.setUp(UIDStreamTest.java:52)

              So it would appear that the junit specialised ClassLoader does something icky.

              Using the text UI or the swing UI with the -noloading flag (and making sure that everything has cleanly rebuilt and deployed) and not using interfaces packaged into client jars, but just having a directory tree with .class files in it, gets things going.