5 Replies Latest reply on Mar 23, 2014 6:53 PM by Antonio Rossi

    Remote ejb causes ClassCastException in object returned by lookup()

    Hauke Mehrtens Newbie

      I am trying to call a remote EJB from my application running in a JBoss 7.1.2 to an other application running on a different computer also in a JBoss 7.1.2, when both applications are running in the same application server and I am doing a local ejb call it works.


      InitialContext context = new InitialContext(jndiEnv);

      RequestControllerRemote rc = (RequestControllerRemote)context.lookup("Gov2OCSPCRLRelay/management3/RequestControllerBean" + "!" + RequestControllerRemote.class.getName());


      jndiEnv returns the following:

      {java.naming.factory.initial=org.jboss.naming.remote.client.InitialContextFactory, java.naming.provider.url=remote://, java.naming.security.principal=test, java.naming.factory.url.pkgs=org.jboss.ejb.client.naming, java.naming.security.credentials=123456}


      In the bold line I get the following Exception:

      2012-09-24 16:21:28,025 ERROR [RMIRelayConnector] Error obtaining relay RequestController: : java.lang.ClassCastException: $Proxy155 cannot be cast to de.bos_bremen.gov.ocspcrlrelay.requestcontroller.RequestControllerRemote

          at de.bos_bremen.gov2.core.certificatevalidator.worker.RMIRelayConnector.sendRequest(RMIRelayConnector.java:124)


      Instead of the cast I also tried to use PortableRemoteObject.narrow, but this also caused a ClassCastException.

      narrowed = PortableRemoteObject.narrow(rc, RequestControllerRemote.class);


      The object returned from the lookup implements the methods declared in the interface and it also implements a interface with the name of the interface I want to cast it to.


      It looks like the interface the returned object implements is in an other class loader than the interface I want to cast it to.


      When adding a "ejb:" infront of the jndi name I get a Exception from the server that he can not find the ejb.


      Does anyone had any success with remote ejb calls? I haven't tried using jboss-ejb-client.properties as descripted in https://github.com/jboss-jdf/jboss-as-quickstart/tree/master/ejb-remote , because I want to provide the remote url in my code and not in a static properties file.

        • 1. Re: Remote ejb causes ClassCastException in object returned by lookup()
          robert.geisler Newbie



          do you set

          jndiEnv.put("jboss.naming.client.ejb.context", true);





          • 2. Re: Remote ejb causes ClassCastException in object returned by lookup()
            Hauke Mehrtens Newbie

            Thanks for the hint.

            I haven't set jboss.naming.client.ejb.context and I will try it out when I am back in the office.

            • 3. Re: Remote ejb causes ClassCastException in object returned by lookup()
              Hauke Mehrtens Newbie

              Somehow the cast now works without setting jboss.naming.client.ejb.context. I moved some code and somehow something with the class loaders must have changed.


              Hashtable<String, Object> p = new Hashtable<String, Object>();

              p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");

              p.put(Context.PROVIDER_URL, "remote://");

              p.put(InitialContext.SECURITY_PRINCIPAL, "test");

              p.put(InitialContext.SECURITY_CREDENTIALS, "123456");

              // p.put("jboss.naming.client.ejb.context", true);


              InitialContext c = new InitialContext(p);

              Object lookupRes = c.lookup("Gov2OCSPCRLRelay/management3/RequestControllerBean!" + RequestControllerRemote.class.getName());

              RequestControllerRemote vLookup = (RequestControllerRemote)lookupRes;


              Now I am getting "EJBCLIENT000025: No EJB receiver available for handling" when calling a method on the remote ejb.

              I read in 199457 that I have to add p.put("jboss.naming.client.ejb.context", true); to fix that.


              When adding jboss.naming.client.ejb.context I get the following Exception in new InitialContext(p);:


              2012-09-28 16:35:00,837 ERROR [RMIRelayConnector] Error obtaining relay RequestController: : javax.naming.NamingException: Failed to create remoting connection [Root exception is java.lang.RuntimeException: Failed to setup EJB remote context]

                  at org.jboss.naming.remote.client.ClientUtil.namingException(ClientUtil.java:51)

                  at org.jboss.naming.remote.client.InitialContextFactory.getInitialContext(InitialContextFactory.java:151)

                  at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:667)

                  at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)

                  at javax.naming.InitialContext.init(InitialContext.java:223)

                  at javax.naming.InitialContext.<init>(InitialContext.java:197)

                  at de.bos_bremen.gov2.core.certificatevalidator.worker.RMIRelayConnector.getRemoteEjbNormal(RMIRelayConnector.java:152)

                  at de.bos_bremen.gov2.core.certificatevalidator.worker.RMIRelayConnector.sendRequest(RMIRelayConnector.java:182)


              Caused by: java.lang.RuntimeException: Failed to setup EJB remote context

                  at org.jboss.naming.remote.client.InitialContextFactory.setupEjbContext(InitialContextFactory.java:468)

                  at org.jboss.naming.remote.client.InitialContextFactory.getInitialContext(InitialContextFactory.java:145)

                  ... 90 more

              Caused by: java.lang.reflect.InvocationTargetException

                  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

                  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

                  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

                  at java.lang.reflect.Method.invoke(Method.java:597)

                  at org.jboss.naming.remote.client.InitialContextFactory.setupEjbContext(InitialContextFactory.java:448)

                  ... 91 more

              Caused by: java.lang.SecurityException: EJBCLIENT000021: EJB client context selector may not be changed

                  at org.jboss.ejb.client.EJBClientContext.setSelector(EJBClientContext.java:186)

                  at org.jboss.naming.remote.client.ejb.RemoteNamingEjbClientContextSelector.setupSelector(RemoteNamingEjbClientContextSelector.java:28)

                  ... 96 more

              • 4. Re: Remote ejb causes ClassCastException in object returned by lookup()
                emollient mind Newbie

                the only problem I see is the jndi name has to follow the new format which is


                ejb:app name/module name/ distinct name/ bean name!package prefixed remote interface


                so if I have

                     an ear file named : EtassEnterprise

                     an ejb jar file named :  EtassBeans

                      distinct name          :

                     bean Name             :  CalculatorBean


                     remote interface      :   org.melsoft.beans.Calculator


                then my lookup code should look like



                public void ejbLookup() {



                        // setup 'base' jndi properties - no jboss-ejb-client.properties being picked up from classpath!

                        Hashtable jndiProps = new Hashtable();


                        jndiProps.put("java.naming.factory.initial", org.jboss.naming.remote.client.InitialContextFactory.class.getName());

                        jndiProps.put(InitialContext.PROVIDER_URL, "remote://localhost:4447");

                        jndiProps.put("jboss.naming.client.ejb.context", true);


                        jndiProps.put(Context.SECURITY_PRINCIPAL, "abc");

                        jndiProps.put(Context.SECURITY_CREDENTIALS, "123");

                        jndiProps.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");



                        System.out.println("\n\nLooking up remote ejb via its jndi name");


                        String ejbJndiName = "ejb:EtassEnterprise/EtassBeans//CalculatorBean!org.melsoft.beans.Calculator";


                        Context ctx = null;

                        try {

                            ctx = new InitialContext(jndiProps);


                            Calculator ref = (Calculator)ctx.lookup(ejbJndiName);


                            // call your ejb methods


                        } catch (NamingException e) {



                        } finally {

                            if (ctx != null) {

                                try {


                                    System.out.println("...Context closed");

                                } catch (Exception e) {}






                  Hope this helps

                • 5. Re: Remote ejb causes ClassCastException in object returned by lookup()
                  Antonio Rossi Newbie


                  i have your identical problem ... have you a solution?


                  i spent  3 days reading any kind of tutorial without solution.