Ruben Laguna's blog

Aug 6, 2009 - 2 minute read - Comments - cglib classloader classnotfoundexception enhancer factory java module netbeans proxy rcp stacktrace

CGLib, NetBeans Modules and class loaders

My first try at using CGLib from a Netbeans RCP ended up in a ClassNotFoundException for net.sf.cglib.proxy.Factory

I had two modules, MainModule and cglib and MainModule was depending on cglib. And the following snippet was raising the ClassNotFoundException.

Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(Graph.class);
enhancer.setCallback(NoOp.INSTANCE);
Graph g = (Graph) enhancer.create(new Class[]{FrameListener.class,InterpreterAbstraction.class},new Object[]{fl,ia}) ;

As I later found out from Enhancer documentation and DevFaqClassLoaders it seems that Enhancer it taking the System class loader instead of taking the Module classloader. I explicitly set the classloader used by the Enhancer via the setClassLoader method and now it seems to work.

Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(Graph.class);
enhancer.setClassLoader(getClass().getClassLoader()); //Use the module classloader instead of the system classloader
enhancer.setCallback(NoOp.INSTANCE);
Graph g = (Graph) enhancer.create(new Class[]{FrameListener.class,InterpreterAbstraction.class},new Object[]{fl,ia}) ;

This is the exception stacktrace I got (just for the record):

java.lang.ClassNotFoundException: net.sf.cglib.proxy.Factory
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:319)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
at java.lang.ClassLoader.loadClass(ClassLoader.java:254)
at org.netbeans.ProxyClassLoader.loadClass(ProxyClassLoader.java:257)
Caused: java.lang.ClassNotFoundException: net.sf.cglib.proxy.Factory starting from ModuleCL@56466f1d[com.rubenlaguna.graphbrowser.guess] with possible defining loaders [ModuleCL@4808e0e1[com.rubenlaguna.graphbrowser.cglibmodule]] and declared parents []
at org.netbeans.ProxyClassLoader.loadClass(ProxyClassLoader.java:259)
at java.lang.ClassLoader.loadClass(ClassLoader.java:254)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:402)
Caused: java.lang.NoClassDefFoundError: net/sf/cglib/proxy/Factory
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:703)
Caused: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at net.sf.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:384)
at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:219)
Caused: net.sf.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException-->null
at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:237)
at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
at net.sf.cglib.proxy.Enhancer.create(Enhancer.java:304)
at com.rubenlaguna.graphbrowser.mainmodule.CircularLayout.performGraphLayout(CircularLayout.java:81)
at org.netbeans.api.visual.graph.layout.GraphLayout.layoutGraph(GraphLayout.java:116)
at org.netbeans.api.visual.layout.LayoutFactory$1.performLayout(LayoutFactory.java:284)
at org.netbeans.api.visual.layout.SceneLayout$LayoutSceneListener.sceneValidated(SceneLayout.java:122)
at org.netbeans.api.visual.widget.Scene.validate(Scene.java:420)
at org.netbeans.api.visual.widget.SceneComponent.addNotify(SceneComponent.java:92)
at java.awt.Container.addImpl(Container.java:1068)
at java.awt.Container.add(Container.java:927)
at com.rubenlaguna.graphbrowser.mainmodule.GraphTopComponent.setDotFile(GraphTopComponent.java:182)
at com.rubenlaguna.graphbrowser.mainmodule.DotFileOpenSupport.createCloneableTopComponent(DotFileOpenSupport.java:45)
at org.openide.windows.CloneableOpenSupport.openCloneableTopComponent(CloneableOpenSupport.java:197)
at org.openide.windows.CloneableOpenSupport$1.run(CloneableOpenSupport.java:98)
at org.openide.util.Mutex.doEvent(Mutex.java:1335)
at org.openide.util.Mutex.writeAccess(Mutex.java:452)
at org.openide.windows.CloneableOpenSupport.open(CloneableOpenSupport.java:95)
at org.openide.actions.OpenAction.performAction(OpenAction.java:81)
at org.openide.util.actions.NodeAction$DelegateAction$1.run(NodeAction.java:589)
at org.netbeans.modules.openide.util.ActionsBridge.doPerformAction(ActionsBridge.java:77)
at org.openide.util.actions.NodeAction$DelegateAction.actionPerformed(NodeAction.java:585)
at org.openide.explorer.view.TreeView$PopupSupport.mouseClicked(TreeView.java:1515)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:253)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:252)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:252)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:252)
at java.awt.Component.processMouseEvent(Component.java:6304)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:6066)
at java.awt.Container.processEvent(Container.java:2085)
at java.awt.Component.dispatchEventImpl(Component.java:4667)
at java.awt.Container.dispatchEventImpl(Container.java:2143)
at java.awt.Component.dispatchEvent(Component.java:4497)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4600)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4273)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4194)
at java.awt.Container.dispatchEventImpl(Container.java:2129)
at java.awt.Window.dispatchEventImpl(Window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4497)
[catch] at java.awt.EventQueue.dispatchEvent(EventQueue.java:635)
at org.netbeans.core.TimableEventQueue.dispatchEvent(TimableEventQueue.java:104)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

References