java.lang.NoClassDefFoundError: for subclasses
Hi,
This is just a question about something I don't understand.
OK, here goes.
1) I have an interface called Animal and a class called Mammal that implements Animal. They are in different jar files. Neither jar file is on the class path. An application tries to create an instance of Mammal and I get a java.lang.NoClassDefFoundError: Mammal.
2) I put the jar that contains the Mammal class on the classpath. An application tries to create an instance of Mammal. I still get java.lang.NoClassDefFoundError: Mammal.
3) I also put the jar with the Animal interface onto the class path. An application tries to create an instance of Mammal and succedes.
My question: Why does the second example, where Mammal IS on the claspath (but Animal is not) still throw a java.lang.NoClassDefFoundError: for the class Mammal ?
I'd love to know,
Many thanks in advance
Russell
Why do you expect that Mammal should be able to access an Animal interface that's not on the classpath - ie, isn't known to Java?
I agree that the message you report receiving is not "exact" in its terminology, but I think it's not reasonable to have errors that report every possible nuance of a problem.
> My question: Why does the second example, where
> Mammal IS on the claspath (but Animal is not) still
> throw a java.lang.NoClassDefFoundError: for the class
> Mammal ?
Realistically, it is probably because the VM creators decided not to implement that level of granularity.
Although I suppose they could have done it that way just to make it less confusing. After all if it said 'Animal' not found and you looked at the line of code where there definitely wasn't a 'Animal' it could be confusing.
The JLS just says that it has to throw that exception if it can't load the class. It doesn't specify that it has to tell you anything at all (even 'Mammal'.)
>>Why do you expect that Mammal should be able to access an Animal interface that's not on the classpath
I don't. As far as accessing classes goes the examples in my question behaved exactly as I would expect.
I asked a question about error reporting not class loading. If the jvm "knows" that Mammal is not on the class path (which it does) then why does it not know that Animal is not on the classpath was my question ?
Thanks for your help, I wasn't having a go at the JVM or maligning java in anyway. The behaviour I describe points at something specific going on in the way classes are loaded and I was curious as to what that is.