I wrote my own classloader, which works with classes, which implements interface Plugin. But I can't cast Class to Plugin. What's wrong?
public Plugin load(String pluginName, String pluginClassName) {
SimpleClassLoader system = new SimpleClassLoader();
system.setClassName(pluginClassName);
Plugin aClass = null;
try {
aClass = (Plugin) system.loadClass(pluginRootDirectory + pluginName + "\\" + pluginClassName + ".class");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return aClass;
}
Error:(18, 47) java: incompatible types: java.lang.Class cannot be converted to Plugin
I addition this is the main part of my SimpleClassLoader class which extends ClassLoader.
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
byte[] bytes;
try {
bytes = Files.readAllBytes(new File(name).toPath());
} catch (IOException e) {
throw new ClassNotFoundException();
}
return defineClass(className, bytes, 0, bytes.length);
}