1 Reply Latest reply on Sep 25, 2001 4:24 AM by callumw

    Classloading issue causes ClassCastException for log4j

    callumw Newbie

      Hi guys,

      Has anyone had problems integrating Log4j into their applications under 2.4.0?

      We started with JBoss-Tomcat 2.2.2/3.2.2 and extended the Category class in Log4j as suggested by the people from Log4j. This is done in a similar way to how JBoss extends the Category class in version 2.4.0 (in fact they happen to be almost identical, which is reassuring). The extended Category is then made a static variable in all classes that use logging and obtain an instance of the Category using

      public SomeClass {

      static MyCategory cat = (MyCategory) MyCategory.getInstance(SomeClass.class.getName());

      All seemed fine under that version of JBoss. We also found it was better (everything worked more reliably than when deploying all to the deploy directory) to deploy the EJBs to the jboss/deploy and the WARs to the tomcat/webapps directories separately.

      Now attempting to upgrade the code base to version 2.4.0 causes a problem. It appears that under the old version of JBoss, the MyCategory class used in the attribute of SomeClass was loaded using the org.apache.tomcat.loader.AdaptiveClassLoader as was the instance obtained from the getInstance call on MyCategory.
      However, under version 2.4.0 it appears that initially both the SomeClass attribute and getInstance MyCategory's are loaded under the AdaptiveClassLoader but then when it comes to some other classes (say AnotherClass) the instance returned from the getInstance is loaded using the AdaptiveClassLoader but the AnotherClass attribute is loaded using the java.net.URLClassLoader causing a java.lang.ClassCastException.

      It appears that the class loading hierarchy or architecture has changed enough that it disrupts the function of our own Log4j extension.

      Its entirely possible of course that we are using it incorrectly or that some new service should be used as an alternative. Log4j, although its a great tool, has always caused a problem with dynamic loading due to the static nature of its use, but it would be useful to find out exactly how it should be used in JBoss.



        • 1. Re: Classloading issue causes ClassCastException for log4j
          callumw Newbie

          Sorry to answer my own question...

          This issue seems to be resolved when bundling the EJB JAR and WAR into an EAR. The EAR also contains all my support JARs (including my log4j.jar) which are still referenced from the manifest files of my other EJB JAR etc. The WAR file still contains a WEB-INF/lib containing its copy of the support JARs.

          The EAR is deployed into jboss/deploy with no other files needed during deployment. It also works with security turned on.

          I have tested this on versions 2.2/2.4.0/2.4.1

          Happy coding