Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This reminds me of an article I read 10 years ago about the classloading arrangements in Java. It's still there <a href="http://www.javaworld.com/javaqa/2003-06/01-qa-0606-load.html" rel="nofollow">on JavaWorld</a>.</p> <p>The article won't answer your question directly, but it may help understand your problem. You need to cause <code>MyClass</code> to be loaded through your custom class loader and trump the default class loading behavior, which is to first delegate class loading to the parent classloader and only attempt to load a class if that fails.</p> <p>Allowing <code>MyClass</code> to get loaded by a classloader other than yours will store a relationship from the instantiated class to that classloader (via <code>getClassLoader</code>) and cause Java to use that other classloader to try to discover any referenced classes <a href="http://docs.oracle.com/javase/specs/jvms/se5.0/html/ClassFile.doc.html#20080" rel="nofollow">found at compile time</a>, effectively bypassing your custom class loader by virtue of the class loader hierarchy and the delegation model. If <code>MyClass</code> is instead <em>defined</em> by your class loader, you get a second chance.</p> <p>It sounds like a job for something like <code>URLClassLoader</code>, overriding <code>loadClass</code> and trumping the delegation model for classes residing in your JARs. You'll probably want to use a bootstrap approach (as suggested by Thomas in a comment above) to force a single entrypoint class to be loaded through your custom class loader, dragging all the others with it.</p> <p>Also informative is <a href="http://www.javaworld.com/javaworld/javaqa/2003-03/01-qa-0314-forname.html" rel="nofollow">this other JavaWorld article</a> by the same guy, which warns you about the caveats of Class.forName. That too may trip your classloading arrangements.</p> <p>I hope this helps and proves informative. In any case, it sounds like a difficult solution that is easy to break as your code evolves.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload