wood burning stoves 2.0*
The moose likes Java in General and the fly likes java.lang.NoClassDefFoundError When trying to load a class which is dependant on a class inside jar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "java.lang.NoClassDefFoundError When trying to load a class which is dependant on a class inside jar" Watch "java.lang.NoClassDefFoundError When trying to load a class which is dependant on a class inside jar" New topic
Author

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

Andy Young
Greenhorn

Joined: Nov 03, 2008
Posts: 9
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

Joined: Feb 25, 2007
Posts: 1638

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.


apigee, a better way to API!
Andy Young
Greenhorn

Joined: Nov 03, 2008
Posts: 9
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: java.lang.NoClassDefFoundError When trying to load a class which is dependant on a class inside jar
 
Similar Threads
Class loading in JBoss
Own URLClassloader...
JAR files cached in JVM ?
Inherit/modify class at runtime
URLClassLoader won't free JAR files