13 Replies Latest reply on Jun 13, 2005 10:17 AM by Fairoz Sharif

    ClassCastException with LocalHome casting

    Fang Yidong Newbie

      I have a CommandEJB,which local interface and home is CommandLocal and CommandLocalHome.I use this CommandEJB to execute classes that implements the Command interface:

      interface Command{
      void execute() throws CommandExecuteException;
      }

      here is the CommandEJB:

      public class CommandEJB implements SessionBean {
      ....
      public void execute(Command command) throws CommandException{
      command.execute();
      }
      }


      I put CommandEJB.class,Command.class,CommandException.class,CommandLocal.class and CommandLocalHome.class into my-ejb.jar and deploy it as an EJB.

      And I put Command.class,CommandException.class,CommandLocal.class and CommandLocalHome.class into my-client.jar and place it into web module's WEB-INF/lib.

      Now I test it from my test.jsp:

      1 Context ctx=new InitialContext();
      2 Object home1=ctx.lookup(CommandLocalHome.JNDI_NAME);
      3 out.println(home1.getClass().getInterfaces()[0]);
      4 CommandLocalHome home2=(CommandLocalHome)home1;

      If line 4 is deleted, the jsp runs successfully,but if line 4 exists,ClassCastException will be raised.

      Is it a bug of JBoss' class loader?
      How to resolve it?

      Thanks.



        • 1. Re: ClassCastException with LocalHome casting
          Marc Mayr Newbie

          i'm not sure if i'm understanding your problem fully (i'm just a beginner) but i think i've read somewhere, that for some reason you have to cast a a class here in that way:

          home 2 = (CommandLocalHome) PortableRemoteObject.narrow(home1,CommandLocalHome.class);


          hope this helps (at least to find a solution) ;)

          • 2. Re: ClassCastException with LocalHome casting
            Rajeev Sood Newbie

            There is no need to narrow on a LocalHome, only with a RemoteHome. The problem is probably that JBoss, as of 4.0.2, uses the Tomcat class loader for anything hosted by Tomcat, including web services and JSPs. Prior to this, they used the JBoss Class Loader by default. You have a few different options with regards to how to handle this, so here's the wiki page that should help:

            http://wiki.jboss.org/wiki/Wiki.jsp?page=ClassLoadingConfiguration

            and JIRA issue page:
            http://jira.jboss.com/jira/browse/JBAS-1691

            Personally, I just set Tomcat to use the JBoss class loader, but this is probably just a stopgap fix. I think I understand why it was changed, but am not sure I'm happy with the call-by-value method of dealing with this. Does anybody have any other solutions that might work. I'm currently publicly exposing a JAX-RPC web service, which in turn calls a stateless session bean. I need the web service for .NET client integration. Thanks in advance for any help that anyone can offer.

            Raj

            • 3. Re: ClassCastException with LocalHome casting
              Fang Yidong Newbie

              I've solve this problem by changing options of jboss-service.xml in deploy/jbossweb-tomcat55.sar:

               <attribute name="Java2ClassLoadingCompliance">true</attribute>
               <attribute name="UseJBossWebLoader">true</attribute>
              


              This is a class loader issue of Java2.

              • 4. Re: ClassCastException with LocalHome casting
                Rajeev Sood Newbie

                 

                "movzx" wrote:

                This is a class loader issue of Java2.


                Not really, this is an artifact of the Servlet 2.3 spec, which calls for a web container first class loading.

                I haven't tried it yet, but upon rereading the JIRA issue discussion I mentioned earlier, I believe that the more appropriate way to solve my problem is to instead add the session bean interface packages to the Filtered packages list in the file that movzx referenced earlier. Anyone try this yet?

                Thanks

                Raj

                • 5. Re: ClassCastException with LocalHome casting
                  Fang Yidong Newbie

                  erinol0,so you mean that you will call a SLSB from the web module? If so,that will be the same problem as I've mentioned before. I believe you will get a ClassCastException if your web module and the EJB module don't belong to the same EAR while trying to pass instances by reference,for the Java2 defines a unique class as the combination of its classloader and its FQN.

                  • 6. Re: ClassCastException with LocalHome casting
                    Rajeev Sood Newbie

                    A stateless session bean can be called from a web module. I had in fact done this with Sun prior to trying JBoss. Also, my jar and war were even in the same ear.

                    The issue is just that JBoss changed the default behavior of their web container to follow the servlet spec by default, which is why the settings you mentioned changing previously removed the ClassCastException. You basically set JBoss back to 4.0.1 settings.

                    I did have the same problem as you, and that's why I posted the response to your question. I just wasn't convinced that the solution that I chose (to use the JBoss unified class loader for Tomcat) was appropriate. The JBoss developers changed it for a reason, after all.

                    Incidentally, I tried adding the packages of the classes present in my session bean interface in a comma seperated list to the filtered packages list in the tomcat jboss-service.xml file and setting the class loader properties back to false, and that works as well.

                    Actually, I'd really appreciate it if a JBoss developer would comment on this issue, and what is an appropriate course of action to deal with this issue (other than pass by value, because that's CPU intensive).

                    Thanks,

                    Raj

                    • 7. Re: ClassCastException with LocalHome casting
                      sudhakar kanakaraj Newbie

                      true
                      true

                      Yes! This's been workin fine for classcastexception
                      problem in JBoss 4.X and +,

                      thanx a lot!
                      sudhakar kanakaraj

                      • 8. Re: ClassCastException with LocalHome casting
                        sudhakar kanakaraj Newbie

                        I Meant for movzx reply on

                        I've solve this problem by changing options of jboss-service.xml in deploy/jbossweb-tomcat55.sar:

                        sudhakar kanakaraj

                        • 10. Re: ClassCastException with LocalHome casting
                          Rajeev Sood Newbie

                           

                          "scott.stark@jboss.org" wrote:
                          And everyone asking this question has read:
                          http://wiki.jboss.org/wiki/Wiki.jsp?page=JBossClassLoadingUseCases


                          Scott,

                          I read the article, and it was very helpful. However, it doesn't yet cover UCL3 and EARs with embedded JARs and WARs (the second TO-DO), which is the situation I'm dealing with.

                          So, extrapolating from what you wrote in the use cases wiki page, I would need to somehow make the EAR load the session bean's interface classes. That way they would be available to both the JAR and WAR.

                          Is this correct? If so, how do I do that? If not, what should I do?

                          Thanks.

                          Raj

                          • 11. Re: ClassCastException with LocalHome casting
                            Scott Stark Master

                            There can only be a single class loader for types used in call be reference scenarios. Wars are always at bottom of the class loader chain, so this means that they should never contain duplicate classes that are involved in call by reference scenarios.

                            • 12. Re: ClassCastException with LocalHome casting
                              Rajeev Sood Newbie

                              Thanks Scott.

                              I just wanted to rephrase what you said to clarify. It is imperative that you do not include classes in both a war and jar in the same ear as they will be loaded by two different class loaders, thus causing the ClassCastException. If they are present only once in the ear, they will be loaded and available in the unified loader repository and the war will grab the loaded references when an attempt to load them from the war fails. You do not need to modify the jboss-service.xml file.

                              Now that I understand how JBoss class loading works, I find it preferrable to the Sun implementation, which required you to put the file in both the jar and war in order for it to work.

                              Thanks for the help and the great product.

                              Raj




                              • 13. Re: ClassCastException with LocalHome casting
                                Fairoz Sharif Newbie

                                Lots of suggestion but i didnt find any thing helpfull , simple i got a .war folder which is suppoesed to access a bean deployed separately within jar file i tried by giving ejb-local-ref tag in deployment descriptor of ejb module but nothing changed, i tried by adding ejb-localref adding jboss-web.xml nothing helpfull can any suugest what should i do to make it work