Google

Feb 24, 2014

Troubleshooting Java classloader or class loading issues -- Weblogic example

Every Java developer faces the following issue from time to time.

"I thought that I was using my application package version of a library but apparently my application server (e.g. Weblogic, JBoss) has already loaded an older version of this-library-issue"

These issues normally arise when there are certain classes which are packaged along with Application Server as part of common libraries and the same set of classes are also present in our Application package as well (normally inside WEB-INF/lib folder of the application). The reasons for the problem lies with how the class loading works in any Application Server. Different Application Servers provide different mechanisms to deal with this problem. 

Recently I faced the similar issue with loading my MOXy libraries for marshaling and marshaling XML to and from Java objects. Troubleshooting these issues could be tricky. and luckily the eclipse link MOXy library had a org.eclipse.persistence.Version.getVersion( ), which prints the version of the library used. In my case, an older version was used from the Weblogic lib directory, ignoring the later version packed in my war or ear package.

Solving it when you deploy a war file in Weblogic server.

The web logic specific web deployment descriptor file weblogic.xml file shown below hinting weblogic to use the jar libraries included the WEB-INF/lib for the listed packages like org.eclipse.persistence.*.



<?xml version="1.0" encoding="UTF-8"?>
<weblogic-web-app xmlns="http://xmlns.oracle.com/weblogic/weblogic-web-app" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd http://xmlns.oracle.com/weblogic/weblogic-web-app http://xmlns.oracle.com/weblogic/weblogic-web-app/1.4/weblogic-web-app.xsd">

 <context-root>wrapng</context-root>

 <container-descriptor>
  <prefer-application-packages>
         <package-name>javax.el.*</package-name>
         <package-name>com.sun.el.*</package-name>
         <package-name>javax.faces.*</package-name>
   <package-name>com.sun.faces.*</package-name>
   <package-name>org.joda.*</package-name>
   <package-name>org.apache.*</package-name>
      <package-name>antlr.*</package-name>
      <package-name>com.sun.xml.bind.*</package-name>
   <package-name>javax.servlet.jsp.jstl.*</package-name>
   <package-name>org.eclipse.persistence.*</package-name>
     </prefer-application-packages>
 </container-descriptor>
 
 <fast-swap>
  <enabled>false</enabled>
 </fast-swap>
 
 <jsp-descriptor>
  <compress-html-template>true</compress-html-template>
 </jsp-descriptor>
 
</weblogic-web-app>


Solving it when you deploy an ear file in Weblogic server.

Similarly, if you have an ear file, and the the library is packaged within /lib folder. The ear descriptor file weblogic-application.xml  will look like

<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-application xmlns:wls="http://www.bea.com/ns/weblogic/weblogic-application" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
   http://java.sun.com/xml/ns/javaee/javaee_5.xsd 
   http://www.bea.com/ns/weblogic/weblogic-application 
   http://www.bea.com/ns/weblogic/weblogic-application/1.0/weblogic-application.xsd">
 <wls:application-param>
  <wls:param-name>webapp.encoding.default</wls:param-name>
  <wls:param-value>UTF-8</wls:param-value>
 </wls:application-param>
 
 <wls:prefer-application-packages>
        <wls:package-name>antlr.*</wls:package-name>
        <wls:package-name>org.eclipse.persistence.*</wls:package-name>
    </wls:prefer-application-packages>
 
</wls:weblogic-application>



Refer to Weblogic documentation for other options like "prefer-web-inf-classes" element. 

Labels: ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home