1 Reply Latest reply on Jun 18, 2006 2:17 PM by Chris Bredesen

    ClassCastException after JNDI lookup

    Walter Pallestrong Newbie


      I really hope someone can help me.
      I have been reading trough a lot of faqs and forums but it
      simply seams that I am not able to solve this newbie problem.
      I am newbie using EJB3 (and EJB in general).
      I installed JBOSS 4.0.4GAPatch1 on my windows xp sp2 machine.
      I am trying to deploy a Stateless session bean; a typical HelloWorldBean.
      Following information:

      jmx-console show
      +- UserTransaction (class: org.jboss.tm.usertx.client.ClientUserTransaction)
      +- HelloWorldBean (class: org.jnp.interfaces.NamingContext)
      | +- local (proxy: $Proxy60 implements interface ejb.HelloWorldLocal,interface org.jboss.ejb3.JBossProxy,interface javax.ejb.EJBLocalObject)
      | +- remote (proxy: $Proxy59 implements interface ejb.HelloWorldRemote,interface org.jboss.ejb3.JBossProxy,interface javax.ejb.EJBObject)

      My HelloWorldBean

      public class HelloWorldBean implements ejb.HelloWorldRemote, ejb.HelloWorldLocal{

      /** Creates a new instance of HelloWorldBean */
      public HelloWorldBean() {

      public String getHelloWorld() {
      return "Hello World!";


      My java client

      public class Main {

      /** Creates a new instance of Main */
      public Main() {


      public static void main(String[] args) {

      Context context = null;

      try {
      context = getInitialContext();
      } catch (NamingException e) {
      // TODO Auto-generated catch block
      String myBean = "HelloWorldBean";
      Object hello = null;
      try {
      hello = context.lookup(myBean);
      } catch (NamingException e) {
      // TODO Auto-generated catch block
      System.out.println(myBean + " is bound to " + hello);
      Class[] c = hello.getClass().getInterfaces();
      for (int i=0;i<c.length;i++){

      HelloWorldRemote helloSession = null;

      try {
      helloSession = (HelloWorldRemote)PortableRemoteObject.narrow(hello, HelloWorldRemote.class);
      } catch (ClassCastException e) {


      public static Context getInitialContext()
      throws javax.naming.NamingException {
      Properties p = new Properties();
      return new InitialContext(p);

      The error output

      HelloWorldBean is bound to org.jnp.interfaces.NamingContext@111a3a4
      at com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:229)
      at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:137)
      at Main.main(Main.java:62)
      Caused by: java.lang.ClassCastException: org.jnp.interfaces.NamingContext
      at com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:212)
      ... 2 more

      I do not use any jboss.xml or other xml descriptors. I hope that is ok, as i can lookup the bean at least; Please correct me if I am wrong.

      So as far as I can see, I can lookup the context; but I have no idea why it fails then.

      Any ideas.