0

I am writing a classloader for a long-running server instance. If a user has not yet uploaded a class definition, I throw a ClassNotFoundException; seems reasonable.

The problem is this: There are three classes (C1, C2, and C3). C1 depends on C2, C2 depends on C3. C1 and C2 are resolvable, C3 isn't (yet). C1 is loaded. C1 subsequently performs an action that requires C2, so C2 is loaded. C2 subsequently performs an action that requires C3, so the classloader attempts to load C3, but can't resolve it, and an exception is thrown. Now C3 is added to the classpath, and the sequence is restarted (starting from the originally-loaded C1). The issue is, C2 seems to remember that C3 couldn't be loaded, and doesn't bother asking the classloader to find the class... it just re-throws the memoized exception.

Clearly I can't reload C1 or C2 because other classes may have linked to them (as C1 has already linked to C2).

I tried throwing different types of errors, hoping the class might not memoize them. Unfortunately, no such luck.

Is there a way to prevent the loaded class from binding to the exception? That is, I want the classloader to be allowed to keep trying if it didn't succeed the first time.

Thanks!

Jim
  • 2,111
  • 4
  • 25
  • 34
  • http://stackoverflow.com/questions/1457863/what-is-the-difference-between-noclassdeffounderror-and-classnotfoundexception thread might be useful. Looks like you should throw NoClassDefFoundError. – andbi Jan 08 '11 at 21:43
  • Java automatically converts ClassNotFoundExceptions into NoClassDefFoundErrors at link time when implicitly loading classes. Regardless, the same memoized exceptions problem occurs. – Jim Jan 08 '11 at 21:50

1 Answers1

0

No, it is not possible. Section 2.17.2 of the JVM specification:

If an error occurs during class loading, then an instance of one of the following subclasses of class LinkageError will be thrown at any point in the program that (directly or indirectly) uses the type:

...

  • NoClassDefFoundError: No definition for a requested class or interface could be found by the relevant class loader.
Brett Kail
  • 33,593
  • 2
  • 85
  • 90