Re: ClassLoader not loading recompiled classes
Aryeh M. Friedman <Aryeh.Friedman@gmail.com> wrote:
if (lastMod > classTimestamp) {
// if classfile is new, load it.
System.out.println("(re)loading MessageGeneratorImpl");
Object o = new URLClassLoader(
new URL[] {new File("../generator").toURL()},
Reloader.class.getClassLoader()).
loadClass("MessageGeneratorImpl").newInstance();
generator = (MessageGenerator)o;
classTimestamp = lastMod;
}
System.out.println("message: " + generator.getMessage());
Thread.currentThread().sleep(10 * 1000);
This is functionally identical to the following (which is the test
code that lead me to make the comment about externally compiled
classes):
Sure, there's lots of ways to do it. However, if you're having trouble with
classes not getting reloaded, my example shows that it has NOTHING to do with
casting before use, or "external" compilation.
new timestamps *DO NOT* help.
They're just one possible mechanism for determining that the class has
changed. I used them because someone said they were having problems with
reloading classes and wanted to detect when a file changed.
.. the only solution I have found is to
read the binary class file and then link it to symbol table with
ClassLoader.defineClass (btw this is the solution JUnit uses)).
JUnit may do that for it's own convenience, and perhaps for bytecode
manipulation as it loads stuff. Or maybe so it can force loading classes that
have the same package and classname as something the parent classloader can
see. It's absolutely not necessary for normal class loading.
--
Mark Rafn dagon@dagon.net <http://www.dagon.net/>