Próbuję napisać funkcję, który zeskanuje jara, pobierze z niego klasę o nazwie ***CommandExecutor i zwróci obiekt tej klasy. Klasę pobrałem, ale nie potrafię sobie poradzić ze stworzeniem instancji tej klasy. W tym końcowym try'u wszystko błyska czerwonością, to tylko raczej próba pokazanie mojego procesu myślowego.
private CommandExecutor parseCommand(Path filePath) {
String path = filePath.toString();
JarFile jar = null;
URL[] urls = null;
try {
jar = new JarFile(path);
urls = new URL[]{ new URL("jar:file:" + path + "!/")};
} catch (IOException e) {
e.printStackTrace();
}
if(jar == null || urls == null)
return null;
URLClassLoader classLoader = new URLClassLoader(urls);
Enumeration<JarEntry> entries = jar.entries();
while(entries.hasMoreElements()){
JarEntry entry = entries.nextElement();
if(entry.getName().contains("CommandExecutor.class")){
String className = entry.getName().substring(0,entry.getName().length()-6); // -6 because of .class
try {
Class<CommandExecutor> clazz = classLoader.loadClass(className);
Constructor<clazz> constructor = clazz.getConstructor();
CommandExecutor executor = constructor.newInstance();
return executor;
} catch (NoSuchMethodException | ClassNotFoundException
| InstantiationException | InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
}
}
}
return null;
}
CommandExecutor to interfejs.
Błędy:
Error:(90, 73) java: incompatible types: java.lang.Class<capture#1 of ?> cannot be converted to java.lang.Class<me.wizziee.pilot.common.CommandExecutor>
- dla pierwszej linijki w try'u
Error:(91, 33) java: cannot find symbol
symbol: class clazz
location: class me.wizziee.pilotserver.Server
- dla drugiej linijki w try'u