Question

I have ejb 2.1 deployed in JBOSS 5 server . The jndi name is ejb/DemoEJB and it matches the jmx-console tree in jboss . While i try to lookup in struts action class as follows , it throws the ClassCastException . kindly let me know, if there is any different way to lookup . The ejb jar is separately deployed and it is not in the ear file.

   Properties env = new Properties();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
        env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
        env.put(Context.PROVIDER_URL, url);
        InitialContext ctx = new InitialContext(env);
DemoEJB demoEjb = (DemoEJB)ctx.lookup("ejb/DemoEJB");

ejb-jar.xml

<?xml version = '1.0' encoding = 'windows-1252'?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">
<ejb-jar>
  <enterprise-beans>
    <session>
      <description>Session Bean ( Stateless )</description>
      <display-name>DemoEJB</display-name>
      <ejb-name>DemoEJB</ejb-name>
      <home>DemoEJB.DemoEJBHome</home>
      <remote>DemoEJB.DemoEJB</remote>
      <ejb-class>DemoEJB.impl.DemoEJBBean</ejb-class>
      <session-type>Stateless</session-type>
      <transaction-type>Container</transaction-type>
    </session>

jboss.xml

<?xml version = '1.0' encoding = 'UTF-8'?>
<!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS 5.0//EN" "http://www.jboss.org/j2ee/dtd/jboss_5_0.dtd">
<jboss>
    <session>
      <ejb-name >DemoEJB</ejb-name>
      <jndi-name>ejb/DemoEJB</jndi-name>
      <local-jndi-name >ejb/DemoEJB</local-jndi-name>
      <ejb-ref>DemoEJB.DemoEJB </ejb-ref>
      <ejb-local-ref>DemoEJB.DemoEJBHome</ejb-local-ref>
    </session>

</jboss>

Stack trace

Was it helpful?

Solution

I think the jndi call would return the DemoEJB.DemoEJBHome class, not the DemoEJB one, hence the classcast exception.

Try:

DemoEJBHome home =  (DemoEJBHome)ctx.lookup("ejb/DemoEJB");
DemoEJB demoEjb = home.create();

OTHER TIPS

after the jboss starts (I assume correctly), try to see whether your EJB is deployed successfully and if yes, where exactly in JNDI tree it resides. I would check a jndi-view from your jmx console (at least it was so in jboss 4.x). This will give you an idea of what to look up in your jndi code.

Of course this can be also a lack of some jar in your client code (where the struts is deployed). So the error occurs during the lookup itself. In this case, like Dr. UnitTest said, the stacktrace will be highly appreciated :) Good luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top