I'm trying to invoke methods of classes that I have copied to my folder (where the other classes already were) but when I run the program, it only displays the methods and the results of the classes that were already in that specific folder. I used Thread.sleep(...) in order to have time to copy thw particular class files that I want to add, let's say, after 5 seconds. Here's my code:
private static ClassLoader cl;
public static void main(String... args) throws NoSuchFieldException,
SecurityException, InstantiationException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException,
InterruptedException, IOException {
File dest = new File("D:\\....\\");
File source = new File("D:\\....\\");
List<String> classNames = new ArrayList<>();
try {
File[] files = dest.listFiles();
for (File f : files) {
classNames.add(f.getName().substring(0, f.getName().indexOf(".")));
}
@SuppressWarnings("deprecation")
URL url = dest.toURL();
URL[] urls = new URL[] { url };
cl = new URLClassLoader(urls);
List<Class<?>> classes = new ArrayList<>();
for (String className : classNames) {
classes.add(cl.loadClass(className));
}
for (Class<?> cla : classes) {
Method[] allMethods2 = cla.getDeclaredMethods();
for (Method method : allMethods2) {
FileUtils.copyDirectory(source, dest); // copies the files from one directory to another
Object obj = cla.newInstance();
method.invoke(obj);
Thread.sleep(5000);
}
}
catch (MalformedURLException e) {
}
catch (ClassNotFoundException e) {
}
}
}
Aucun commentaire:
Enregistrer un commentaire