Feb 27, 2014

Debugging Java Class loading issues

"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 your 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.

A common question many developers ask is that

Where on the file system was my Java class loaded from?

Here are some tips and steps to troubleshoot Java class loading issues.

1. -verbose:class option in your JVM. With the -verbose option all the classes that are loaded are listed, along with the JAR file or directory from which they were loaded. The "class" output shows additional information, such as when superclasses are being loaded, and when static initializers are being run.

2. Creating a Java dump and analyzing the Java dump for class loading issues. The Java dumps are created under following circumstances.
  • When a fatal native JVM error.
  • When the JVM runs out of heaps memory space.
  • When a signal is sent to the JVM (e.g. Control-Break is pressed on Windows, Control-\ on Linux, or kill -3 on Unix)
There are tools like jstack, jmap, hprof, and Eclipse Memory Analyzer (MAT) to analyze the Java dumps.

3.  Some of the libraries provide API to list the version number. For example, The Eclipse link MOXy library provides a method as shown below.

  PrintWriter out = response.getWriter();

4.  The org.jboss.test.util.Debug class has a method  displayClassInfo(Class clazz, StringBuffer results) to display the loaded class details. This is done programmatically. What this class essentially does is


URL loc = MyClass.class.getProtectionDomain().getCodeSource().getLocation();


5. The is an onlime search engine that can list possible jar files in which a particular class file like java.sql.Connection can be found.

6. Finally, using the Unix find and grep commands to list the jar files that has a given class file like "Connection".


find . -name '*.jar' -print0 |  xargs -0 -I '{}' sh -c 'jar tf {} | grep Connection.class &&  echo {}' 

You may also like

Labels: ,


Post a Comment

Subscribe to Post Comments [Atom]

<< Home