1 Reply Latest reply on Apr 12, 2006 6:54 AM by echon

    ClassCastException after lookup

    candychow

      Hello all,

      I am getting start to EJB 3.0 with a HelloWorld, but I got the following exception:
      java.lang.ClassCastException: org.jnp.interfaces.NamingContext

      I am using NetBeans 5.5 Preview, JBoss 4.0.4.CR2 with EJB3.

      Here is my HelloWorldBean:

      package hi;
      
      import javax.ejb.Stateless;
      
      @Stateless(name="HelloWorld")
      public class HelloWorldBean implements HelloWorldRemote {
       public String sayHi (){
       return "Hello world!";
       }
      }
      


      Here is my HelloWorldRemote:

      package hi;
      
      import javax.ejb.Remote;
      
      @Remote
      public interface HelloWorldRemote {
       public String sayHi ();
      }
      


      And then I get the Remote by this:

      Properties props = new Properties();
      props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
      props.setProperty("java.naming.provider.url", new String("jnp://localhost:1099"));
      props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
      InitialContext context = new InitialContext(props);
      hello = (HelloWorldRemote) context.lookup("HelloWorld");
      


      I got the exception when i cast the object to HelloWorldRemote after lookup.

      What is the problem with my code? How should I get the Remote correctly?

      Please help. Thanks!!

      Regards,
      Candy

        • 1. Re: ClassCastException after lookup
          echon

          I would say one problem is the JNDI-Binding you do not specify.

          By standard the JNDI-Binding names are
          EAR-FILE-BASE-NAME/EJB-CLASS-NAME/local,
          EAR-FILE-BASE-NAME/EJB-CLASS-NAME/remote,
          EJB-CLASS-NAME/local, EJB-CLASS-NAME/remote

          or you specify them

          @Remote( { HelloWorldServiceRemote.class })
          @RemoteBinding(jndiBinding = HelloWorldServiceRemote.JNDI)
          


          and in the interface

          public interface HelloWorldServiceRemote {
          
           public final static String JNDI = "de.tum.sebis.dbis.service.HelloWorldServiceRemote";
          
           public String getHelloWorld();
          
          }
          



          The other problem are missing jars in your classpath for your client.


          Regards

          Peter