• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.lang.NoClassDefFoundError When trying to load a class which is dependant on a class inside jar

 
Andy Young
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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
throws

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?


Thanks,
Andy


 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Andy Young
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

Thanks for your help.

Andy
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic