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).