If I load a class from file at runtime using a URLClassLoader:
ClassLoader classLoader = new URLClassLoader(new URL[] { classesUrl }, getClass().getClassLoader());
String name = fileName.replace("\\", ".").replace("/", ".").substring(0, s.lastIndexOf("."));
System.out.println("loading class " + name);
Class c = classLoader.loadClass(name);
System.out.println("loaded " + c.getCanonicalName()); // 1
This seems to work - output at 1
is loaded com.robert.test.NumberUtil
.
If I then try to create an instance of that class using
Class.forName("com.robert.test.NumberUtil");
I get a ClassNotFoundException: com.robert.test.NumberTest
. Is what I'm trying to do possible? Or do I have to use the class at 1
(i.e. use the object returned from classLoader.loadClass()
and once it's out of scope reload it?).