• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stumped on classloading problem! Help!  RSS feed

 
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class "MyClass" which is in my current classpath. This class uses a class ("MyUnknownClass") inside that is not in my classpath but is in a jar "temp.jar". I load the class as follows:



This throws an exception java.lang.NoClassDefFoundError: MyUnknownClass.

What is going on here? I have created the URLClassLoader with the proper jar file (and a test on that URL finds it does exist), so why can't it find the class?
[ July 29, 2005: Message edited by: Dan Bizman ]
 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, now here's something even weirder. If I create the classloader as such:



then it is able to find the classes, but now when I try to cast "MyClass" to the interface it implements, it throws a cast exception (obviously). So what's going on here? Why does it load all the classes with a null parent and not when it has a parent classloader?!

What am I missing? How do I fix this?
 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From what I can see, the problem is that "MyClass" (as it's on the classpath) gets loaded by the system classloader, and since it includes classes that are in the child classloader (which the parent know nothing of), there is no delegation back to it for the unfound classes (in other words, once you delegate to parent for a class, all classes referenced by it are loaded from parent as well).

Is there a way around this?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!