Problème avec classLoader et classe interne
Bonjour,
Pour un projet j'ai du créer un classLoader me permettant de charger des modules à chaud. Cependant j'ai un problème avec l'un des modules qui utilise des classes internes anonymes pour les listener.
Code:
1 2 3 4 5 6 7 8 9
| Exception in thread "main" java.lang.IllegalAccessError: tried to access class ModuleFormule.ModuleFormule$1 from class ModuleFormule.ModuleFormule
at ModuleFormule.ModuleFormule.<init>(ModuleFormule.java:77)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at base.PluginLoader.main(PluginLoader.java:47) |
Voici le PluginLoader. Il contient un petit main pour tester.
C'est au niveau du newInstance que l'exception est levée.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| import java.io.IOException;
import java.io.InputStream;
import java.security.SecureClassLoader;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
public class PluginLoader extends SecureClassLoader {
private String repertoirePlugins = "C:\\javatest\\Module\\";
public Class<?> findClass(String name) throws ClassNotFoundException {
System.out.println("name : "+name);
byte[] b = loadClassData(name);
return super.defineClass(name+"."+name, b, 0, b.length);
}
private byte[] loadClassData(String name) {
try {
JarFile zf = new JarFile(repertoirePlugins+name+".jar");
ZipEntry entry = zf.getEntry(name+"/"+name+".class");
System.out.println(entry.getName());
InputStream is = zf.getInputStream(entry);
byte[] tab = new byte[is.available()];
is.read(tab);
return tab;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static void main(String[] args){
PluginLoader pl = new PluginLoader();
try {
Class c = pl.findClass("ModuleFormule");
IModule mod = (IModule)c.newInstance();
System.out.println(mod.getClass().getDeclaredMethods());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} |
Si quelqu'un a une solution je suis preneuse.
Merci d'avance !