2 Replies Latest reply on Jul 8, 2012 11:15 PM by mikemil

    ClassCastException when jar NOT in WEB-INF/lib but is in EAR/lib

    mikemil

      I am researching migrating our application to AS7.1.1 from 4.2.3.   I have gotten some smaller jars and a war successfully deployed.  My next step was to package up an ejb jar and a war inside an ear.   After reading thru a fair number of forum posts, it seems like one of the stock answers to class loading problems with EARs is to put the jars needed in the ear/lib folder.    I packaged up our jar and war and put all the depended jars in the ear/lib folder and it successfully deployed but when I hit the servlet, I got an ClassCastException.  

       

      The exception came because the jar that is needed by the war was NOT in the WEB-INF/lib folder, I had put it in the ear/lib and expected that to work.   Adding the jar to the WEB-INF/lib folder resolved the issue but I don't quite understand why I had do to that? 

       

      I've read the "Class Loading in AS7" posting several times and figured that the quote below from the "Ear Class Loading" section covered this? 

       

      "Sub deployments (wars and ejb-jars) always have a dependency on the parent module, which gives them access to classes in EAR/lib, however they do not always have an automatic dependency on each other."

       

      So shouldn't the code in my war have access to the jar from ear/lib?

        • 1. Re: ClassCastException when jar NOT in WEB-INF/lib but is in EAR/lib
          jaikiran

          Mike Miller wrote:

           

          but when I hit the servlet, I got an ClassCastException.  

           

          The exception came because the jar that is needed by the war was NOT in the WEB-INF/lib folder, I had put it in the ear/lib and expected that to work.   Adding the jar to the WEB-INF/lib folder resolved the issue but I don't quite understand why I had do to that? 

           

          That doesn't look like a reason for ClassCastException. Can you please post the entire exception stacktrace and also more details about the jar which you moved around? You can also post the output of:

           

          jar -tf yourear.ear

           

          and

           

          jar -tf yourwar.war

          • 2. Re: ClassCastException when jar NOT in WEB-INF/lib but is in EAR/lib
            mikemil

            Not sure what happened but now I cannot recreate the scenario.    I have tried going back to the ear/war combo and removing the jar that was causing the problem but no luck.  

             

            The initial problem was our startup servlet trying to initialize Hibernate 2.   Hibernate was not given a caching provider and so was setting up the default, ehchache.  We got NoClassDefFoundExceptions, followed by the ClassCastException.   The error read something like "Cannot cast from net.sf.ehchache.hibernate.Provider to the net.sf.hibernate.cache.Cache" and it seemed like the problem was ehcache jar was in the ear/lib but not in the WEB-INF/lib folder.   I put the ehcache jar in the WEB-INF/lib folder and the problem went away.

             

            I already deleted all the logs, etc because this happened on Thursday and by Friday, I had moved on to trying our full ear, with 20 jars, and 8 wars as part of the migration research.