I have a program where there user can import code which is compiled at runtime:
System.setProperty("java.home", MainWindow.getOptions()[Config.JDK]); //User has to specify path to JDK
StandardFileMangager fileManager = compiler.getStandardFileManager(null, null, null);
URLClassLoader classLoader = new URLClassLoader(new URL[] { file.getParentFile().toURI().toURL() });
Class<?> loadedClass = classLoader.loadClass(className);
Constructor <?> cons = loadedClass.getConstructor(int.class, File.class);
Object obj = cons.newInstance(pa1, pa2);
classLoader.close();
if (obj instanceof Foo) {
fe = (Foo) obj; //Foo is the interface fe is dependent on
}
I can then work with fe and handle it as a class if the imported code is correct. When I export the project in Eclipse using the option "extract required libraries into generated JAR" this works well and code can be imported and used at runtime.
However, when i use the option "package required libraries into generated JAR" (need to that for obfuscation) and try to import code then, obj is not recognized as instanceof Foo and thus obj cannot be cast to (Foo). Trying to do so would cause a ClassCastException.
I think this has to do with wrong references but I'm unsure how to reference the imported code correctly and tell JVM that obj (if the imported code is correct) actually IS instanceof Foo.
Aucun commentaire:
Enregistrer un commentaire