3 Replies Latest reply on Nov 5, 2009 8:02 AM by jino augustine

    Class cast exception in Ejb Look up

    jino augustine Newbie

      Hi friends,

      Deploying one ejb with JBoss_4_2_2_GA and deploying well.
      But while trying to look up it getting some exception like

      java.lang.ClassCastException: $Proxy78 cannot be cast to stateless.CalculatorRemote

      please help to identify the problem
      is it ejb3 problem or JBoss version problem?

      here is code

      Remote

      package stateless;
      import java.math.*;
      import javax.ejb.Remote;
      import java.lang.annotation.*;
      @Remote
      public interface CalculatorRemote {
       public float add(float x, float y);
       public float subtract(float x, float y);
       public float multiply(float x, float y);
       public float division(float x, float y);
      }
      


      and stateless bean

      and lookup code


      
      public void jspInit() {
       try {
      
       InitialContext ic = new InitialContext();
       calculator = (CalculatorRemote) ic.lookup("example/CalculatorBean/remote");
       System.out.println("Loaded Calculator Bean");
      
       } catch (Exception ex) {
       e.printStackTrace ();
       }
       }
      


        • 1. Re: Class cast exception in Ejb Look up
          jino augustine Newbie

          while calling look up from standard alone application, it will work fine

          
          InitialContext ctx = null;
           Hashtable<String, String> props = new Hashtable<String, String>();
          
           props.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
           props.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
           props.put("java.naming.provider.url", "jnp://localhost:1099");
           try {
           ctx = new InitialContext(props);
           CalculatorRemote calc = (CalculatorRemote) ctx.lookup("CalculatorBean/remote");
           System.out.println( "addition == " + calc .add( 10, 20 ));
          
           } catch (NamingException e) {
           e.printStackTrace();
           }
          
          


          but its not working from servlet
          getting java.lang.ClassCastException: $Proxy78 cannot be cast to stateless.CalculatorRemote
          at web.CalcServlet.init(CalculationServlet.java:36)

          public void jspInit() {
           try {
          
           InitialContext ic = new InitialContext();
          Object object = ic.lookup( "example/CalculatorBean/remote" );
          
           CalculatorRemote calc = (CalculatorRemote)PortableRemoteObject.narrow( object , CalculatorBean.class);
          
           System.out.println("Loaded Calculator Bean");
          
           } catch (Exception ex) {
           e.printStackTrace ();
           }
           }
          


          and i print the method and class name here

          Class Name === $Proxy78
          Method === hashCode
          Method === equals
          Method === toString
          Method === add
          Method === subtract
          Method === multiply
          Method === division
          Method === getAsynchronousProxy
          Method === isProxyClass
          Method === getProxyClass
          Method === newProxyInstance
          Method === getInvocationHandler
          Method === getClass
          Method === wait
          Method === wait
          Method === wait
          Method === notify
          Method === notifyAll

          here i am getting the object of java.lang.reflect.Proxy
          why i am getting this object instead of CalculatorRemote ?
          any thing i want to set in jboss-4.2.2.GA sever ?


          thx for your replay

          • 2. Re: Class cast exception in Ejb Look up
            jaikiran pai Master

            Is this an EAR? If yes, then are you packaging the ejb interfaces both in the .war and the ejb .jar? If yes, then remove those interfaces from the .war.

            http://www.jboss.org/community/wiki/ClassCastExceptions

            • 3. Re: Class cast exception in Ejb Look up
              jino augustine Newbie

              thanks for your kind replay

              No, here i have used in both way ear and separate jar and war

              still getting same class cast exception