Hi again. I recently made a post http://stackoverflow.com/questions/3079280/update-java-code-during-runtime and after a few hours fiddling around with different example codes and reading tutorials I have run into the following problem:
By using a ClassLoader I have been able to change a local variabe from Class MyVar1
to Class MyVar2
during runtime using the code at http://www.exampledepot.com/egs/java.lang/reloadclass.html, but I have been unable to replace that Class MyVar2
with another version of MyVar2
.
Both MyVar1
and MyVar2
implement an interface VarInterface
. The main class holds an instance of the variable using the type VarInterface
.
I have read several other implementations that claim to be correct but I cannot get this to work. Can anyone see what I'm doing wrong here?
Main class loop:
while(true){
i++;
Thread.sleep(1000);
ui.ping();
if(i > 3)
replaceVar();
}
replaceVar:
ClassLoader parentClassLoader = MyClassLoader.class.getClassLoader();
MyClassLoader classLoader = new MyClassLoader(parentClassLoader);
Class newClass = classLoader.loadClass("MyVar2");
ui = (VarInterface)newClass.newInstance();
MyClassLoader.loadClass:
public Class<?> loadClass(String what){
// Get the directory (URL) of the reloadable class
URL[] urls = null;
try {
// Convert the file object to a URL
File dir = new File(System.getProperty("user.dir")
+File.separator+"dir"+File.separator);
URL url = dir.toURL();
urls = new URL[]{url};
} catch (MalformedURLException e) {
}
// Create a new class loader with the directory
ClassLoader cl = new URLClassLoader(urls);
// Load in the class
Class cls = null;
try {
cls = cl.loadClass("MyVar2");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return cls;
}
For the first 3 iterations MyVar1.ping()
is called, after that MyVar2.ping()
is called ad infinitum, even if I replace the MyVar2.class
and MyVar2.java
files.