Sunday, 16. August 2009
Jetty and the NoSuchMethodError (3)
Last time we saw how we can use the ClasspathMonitor of PatternTesting and the JCconsole of the JDK to find classpath problems. But what if the program crashes immediately after you started it? You have no chance to use the JConsole here!

For this case the ClasspathMonitor can be added as shutdown hook:
public static void main(String[] args) throws Exception {
    ClasspathMonitor.addAsShutdownHook();
    ...
}

Each time your program crashes a text file "cpmonxxxxx.txt" is dumped into the temp directory. If you open this file and look for the string "IncompatibleClasses" you will see an entry like this:
...

=== IncompatibleClasses ===
class org.apache.jasper.compiler.Localizer

=== IncompatibleClasspath ===
/Users/oliver/.m2/repository/tomcat/jasper-compiler/5.5.9/jasper-compiler-5.5.9.jar
/Users/oliver/.m2/repository/tomcat/jasper-runtime/5.5.9/jasper-runtime-5.5.9.jar

...
There are two other useful side effects if you register the ClasspathMonitor as shutdown hook:
  • You need not to register it as MBean (it is automatically registered).
  • You'll get classes and classpathes which are never used during the lifetime of your application.
Do you know the Lava Flow Anti Pattern? PatternTesting can help you to find dead classes (and also dead methods) in your code. But this is another story...

... comment