Re: In which circumstances a Class.forName() can fail?
Ivo wrote:
....
The STE is straight from a trace obtained from the current Thread, all
of which is still in scope, so not GCed.
Local variables in the method invocation that threw the exception, and
any others that completed abruptly because of the exception, are now out
of scope. Objects referenced only through those variables, and in
expressions in those methods, may have been collected.
However, "A class or interface may be unloaded if and only if its
defining class loader may be reclaimed by the garbage collector as
discussed in ?12.6. Classes and interfaces loaded by the bootstrap
loader may not be unloaded."
http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.7
That indicates that an unloaded class would also have to involve a
non-bootstrap ClassLoader. Moreover, if the class were loadable by the
bootstrap ClassLoader the forName call would have succeeded, by
initializing the class if necessary.
StackTraceElement does not validate the class name. If the exception had
been modified since being generated, the name might not even really be a
class name. Throwable has a public setStackTrace, so one could put in an
arbitrary StackTraceElement[], not necessarily one generated by
fillInStackTrace.
Have you tried displaying the class name for which the forName call
fails? It would be easy to do from the exception handler, and might
distinguish e.g. an arbitrary String from the name of a class loaded
with a non-bootstrap ClassLoader. Or maybe trigger some entirely new
hypothesis.
Patricia