6 Replies Latest reply on Apr 28, 2010 2:42 AM by NAG Kum

    EJB3: cannot be cast

    Marco Westermann Newbie


      I've a problem and hope anyone can help me.

      I've a EJB3 module deployed to a JBoss JBoss-4.2.2-GA. The module consists the following classes/interfaces:

      package com.zoo;
      import javax.ejb.Remote;
      public interface Tiger {
       public String roar();

      package com.zoo;
      import javax.ejb.Stateless;
      public class TigerBean implements Tiger {
       public String roar() {
       return "Roar!!!";

      After deploying the module the global JNDI Namespace is like:

      +- TopicConnectionFactory (class: org.jboss.naming.LinkRefPair)
       +- jmx (class: org.jnp.interfaces.NamingContext)
       | +- invoker (class: org.jnp.interfaces.NamingContext)
       | | +- RMIAdaptor (proxy: $Proxy47 implements interface org.jboss.jmx.adaptor.rmi.RMIAdaptor,interface org.jboss.jmx.adaptor.rmi.RMIAdaptorExt)
       | +- rmi (class: org.jnp.interfaces.NamingContext)
       | | +- RMIAdaptor[link -> jmx/invoker/RMIAdaptor] (class: javax.naming.LinkRef)
       +- TigerBean (class: org.jnp.interfaces.NamingContext)
       | +- remote (proxy: $Proxy71 implements interface com.zoo.Tiger,interface org.jboss.ejb3.JBossProxy)
       +- HTTPXAConnectionFactory (class: org.jboss.mq.SpyXAConnectionFactory)
       +- ConnectionFactory (class: org.jboss.mq.SpyConnectionFactory)
       +- Tiger (class: org.jnp.interfaces.NamingContext)
       +- UserTransactionSessionFactory (proxy: $Proxy14 implements interface org.jboss.tm.usertx.interfaces.UserTransactionSessionFactory)
       +- HTTPConnectionFactory (class: org.jboss.mq.SpyConnectionFactory)
       +- XAConnectionFactory (class: org.jboss.mq.SpyXAConnectionFactory)
       +- TransactionSynchronizationRegistry (class: com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionSynchronizationRegistryImple)
       +- UserTransaction (class: org.jboss.tm.usertx.client.ClientUserTransaction)
       +- UILXAConnectionFactory[link -> XAConnectionFactory] (class: javax.naming.LinkRef)
       +- UIL2XAConnectionFactory[link -> XAConnectionFactory] (class: javax.naming.LinkRef)
       +- queue (class: org.jnp.interfaces.NamingContext)
       | +- A (class: org.jboss.mq.SpyQueue)
       | +- testQueue (class: org.jboss.mq.SpyQueue)
       | +- ex (class: org.jboss.mq.SpyQueue)
       | +- DLQ (class: org.jboss.mq.SpyQueue)
       | +- D (class: org.jboss.mq.SpyQueue)
       | +- C (class: org.jboss.mq.SpyQueue)
       | +- B (class: org.jboss.mq.SpyQueue)
       +- topic (class: org.jnp.interfaces.NamingContext)
       | +- testDurableTopic (class: org.jboss.mq.SpyTopic)
       | +- testTopic (class: org.jboss.mq.SpyTopic)
       | +- securedTopic (class: org.jboss.mq.SpyTopic)
       +- console (class: org.jnp.interfaces.NamingContext)
       | +- PluginManager (proxy: $Proxy48 implements interface org.jboss.console.manager.PluginManagerMBean)
       +- UIL2ConnectionFactory[link -> ConnectionFactory] (class: javax.naming.LinkRef)
       +- HiLoKeyGeneratorFactory (class: org.jboss.ejb.plugins.keygenerator.hilo.HiLoKeyGeneratorFactory)
       +- UILConnectionFactory[link -> ConnectionFactory] (class: javax.naming.LinkRef)
       +- QueueConnectionFactory (class: org.jboss.naming.LinkRefPair)
       +- UUIDKeyGeneratorFactory (class: org.jboss.ejb.plugins.keygenerator.uuid.UUIDKeyGeneratorFactory)

      After that I developed a test-Client with the following code:
      public class Visitor {
       public void run() {
       try {
       InitialContext ctx = new InitialContext();
       Tiger bean = (Tiger) ctx.lookup("TigerBean");
       catch (Exception e){
       public static void main(String[] args) {
       new Visitor().run();

      Trying to run it I get the following exception:

      java.lang.ClassCastException: org.jnp.interfaces.NamingContext cannot be cast to com.zoo.Tiger

      Can me somebody help with that case.

      Thx Marco

        • 1. Re: EJB3: cannot be cast
          wayne baylor Apprentice

          what are your jndi properties set to? are you using a jndi.properties file?

          • 2. Re: EJB3: cannot be cast
            Marco Westermann Newbie


            yes my jndi.properties looks like:


            • 3. Re: EJB3: cannot be cast
              jaikiran pai Master


              +- TigerBean (class: org.jnp.interfaces.NamingContext)
              | +- remote (proxy: $Proxy71 implements interface com.zoo.Tiger,interface org.jboss.ejb3.JBossPr

              So your lookup string should be TigerBean/remote

              Tiger bean = (Tiger) ctx.lookup("TigerBean/remote");

              • 4. Re: EJB3: cannot be cast
                Marco Westermann Newbie

                Thank you it works..

                I tried several times "TigerBean/Remote" but then I got a NameNotFoundException..

                my fault..

                thanks again


                • 5. Re: EJB3: cannot be cast
                  jaikiran pai Master


                  "MarWestermann" wrote:

                  I tried several times "TigerBean/Remote" but then I got a NameNotFoundException..

                  The JNDI names are case-sensitive. The r in Remote should have been in lower case.

                  "MarWestermann" wrote:

                  Thank you it works..

                  Great !!

                  • 6. Re: EJB3: cannot be cast
                    NAG Kum Newbie

                    Hi Jaikiran,


                    I do have same problem, Pls see below -


                    java.lang.ClassCastException: org.jnp.interfaces.NamingContext cannot be cast to com.tarangtech.mtg.authentication.AuthenticationRemote

                    Here is my lookup code -

                    public static final String RemoteJNDIName =  "AuthenticationBean/remote";
                    AuthenticationRemote authEjb = (AuthenticationRemote)ServiceLocator.getEJB(AuthenticationBean.RemoteJNDIName);
                    System.out.println("EJB Look up successfull ");  

                    my jmx-console -

                     +- AuthenticationBean (class: org.jnp.interfaces.NamingContext)
                      |   +- remote (class: org.jnp.interfaces.NamingContext)
                      |   |   +- UserTransaction[link -> UserTransaction] (class: javax.naming.LinkRef)
                      |   |   +- metaData (class: org.jboss.metadata.client.jboss.JBossClientMetaData)
                      |   |   +- env (class: org.jnp.interfaces.NamingContext)
                      |   |   |   +- com.tarangtech.mtg.authentication.AuthenticationBean[link -> AuthenticationBean/remote] (class: javax.naming.LinkRef)
                      |   |   +- classPathEntries (class: java.util.ArrayList)
                    I have verified for jar file incompatabilities with
                    Jboss server & my application. Don't get the mistake, request you to help me to proceed further...