Java NoClassDefFoundError when calling own class from instrumented method
I am working on a kit of simple Java agents to help me (and hopefully others) troubleshoot Java applications. One of the agents I would like to create instruments the JComponent.getToolTipText() method to quickly identify any开发者_如何学Go GUI class by just hovering the mouse cursor over it.
You can find the code of my transformer and the rest of the project here:
http://sfn.cvs.sourceforge.net/viewvc/sfn/core/src/main/java/org/leplus/sfn/transformer/JComponentTransformer.java?view=markup
I launch my test GUI with the agent attached as follow:
$ java -javaagent:target/jars/sfn-0.1-agent.jar=JComponent -cp lib/jars/bcel-5.2.jar:target/jars/sfn-0.1-test.jar:target/jars/sfn-0.1-agent.jar org.leplus.sfn.test.Main
sfn-0.1-agent.jar contains the org.leplus.sfn.transformer.JComponentTransformer class. sfn-0.1-test.jar contains the org.leplus.sfn.test.Main class.
Here is what the application prints when I launch it and I put the mouse over it:
Loading agent: JComponent Instrumentation ready! Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/leplus/sfn/tracer/ComponentTracer at javax.swing.JComponent.getToolTipText(JComponent.java) at javax.swing.ToolTipManager$insideTimerAction.actionPerformed(ToolTipManager.java:662) ...
What is surprising to me is that if I change my transformer to call any class from the JRE, it works. But it doesn't work when I call my own class org.leplus.sfn.tracer.ComponentTracer. My first guess was a classpath issue but the ComponentTracer is both in the classpath and in the agent's jar. So I am lost.
If any of you see where I am missing something.
Cheers,
Tom
It's a class loader issue. You're instrumenting a class (javax.swing.JComponent) managed by the bootstrap class loader, and having it refer to a class (org.leplus.sfn.tracer.ComponentTracer) managed by the system class loader.
If you put your ComponentTracer class in the bootstrap class loader, the problem should disappear.
java -Xbootclasspath/p:<path/to/jar/containing/ComponentTracer> -javaagent:...
Try running with -DDEBUG as it might show you more information.
Also, I see the target directory here. http://sfn.cvs.sourceforge.net/viewvc/sfn/core/target/ It contains a classes folder, but no jars folder? Check to make sure the jar paths are relative to the project root.
精彩评论