This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I need to be able to create a URLClassLoader at runtime which i then load classes from. I load a number of compiled classes and some jar files into the URLClassLoader like this:
where jars in an array of URLs pointing to jar files and classes is the root directory of some classes.
When i attempt to load a class (in the compiled class directory, NOT in a jar), which uses classes from one or more of the certain jars, i get a java.lang.NoClassDefFoundError.
For example, attempting
because DatatypeAttribute uses SerializationContext (which is in a jar file).
But, if i try then it loads the SerializationContext class with no problems, so i know the class within the jar exists and is accessible, so why cant the call to loadClass("org.dom4j.datatype.DatatypeAttribute") load the DerializationContext class?
Some other classes which i load that use classes from other jar files load correctly.
Does anyone have any idea what can be causing this?
Just make sure that the class "org.dom4j.datatype.DatatypeAttribute" is not loaded by the parent of your URLClassloader.
URLClassloader does not load the class if they are already loaded by the parent.
If it is loaded by the parent then the the control goes to the parent classloader and all the classes loaded inside "org.dom4j.datatype.DatatypeAttribute" will not be using the child classloader. This is the only way, i can think, why you would get the behavior you described.
I found the problem by accident: I refactored some code meaning that i was no longer parsing xml and thus i noticed i had a dom4j.jar on the classpath. removing this meant that it all worked fine. I presume it was getting confused between the classes inside the dom4j.jar and the DatatypeAttribute.class file, probably because like you said, the parent is searching for classes first (when it finds the jar file), and then cannot access the required classes as these are only known to my class loader (the child).