Let me give a little context. This example is me breaking down a complex problem to it's simplest form. I have legacy java code that I am calling from a gradle plugin. This legacy code is widely used across 1000s of people and dozens of projects, so I would like to avoid modifying it. My unit tests on the gradle plugin all work fine, but when I call my plugin task from gradle itself I get this ClassNotFoundException from deep inside this legacy code. I found this odd, so I tried all sorts of different methods to figure out the core problem. Basically I have it down to this snippet of code: http://pastie.org/1900695
The problem in the snipped boils down to the question which class loader should be used to load the class. You want to load the class with the current class loader as defined by your dependencies section. But what actually happens is that the class is loaded by the class loader that loaded the script! These two are very different.
For your snippet you can solve the issue by forcing the correct class loader:
Class testClass = Class.forName("org.apache.xerces.jaxp.DocumentBuilderFactoryImpl", true, getClass().classLoader)
To solve your actual problem, if the Class.forName call happens from within your legacy code, you would have to adjust the Gradle plugin that calls into the code via reflection and use the correct class loader there. Otherwise the wrong class loader will be propagated. Hope that helps.
Cheers, Marco --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email
