I have a list of classes which I need to use many times during the execution of my program, I need to load them dinamically because their code can change very often and I want to treat them as services so that I can update them easily. Here my questions:
Does UrlClassLoader always load the class again even when it hasn't changed? Can this cause performance or memory issues?
Here is my code for testing this:
package classloader;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
public class ClassLoaderClass {
public static void main(String[] args) throws ClassNotFoundException, MalformedURLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, InterruptedException {
//no paramater
Class noparams[] = {};
URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{new URL("file:///C:/Users/hm/Documents/classes/")});
Class gsonClass = urlClassLoader.loadClass("AppTest");
Constructor constructor = gsonClass.getConstructor();
Object Apptest = constructor.newInstance();
Method method = gsonClass.getMethod("printIt",noparams);
Object returnObj = method.invoke(Apptest, null);
String jsonString = (String)returnObj;
}
}
I hope you can help me, thanks.