I am trying to load all classes which are contained in an external jar. I can't include the jar as a library in my project, i have to load it from a unknown location which is defined by the user. So far i am loading the classes using an classloader which extends urlclassloader. Here is the source:
public class JarLoader extends URLClassLoader {
public JarLoader() {
super(new URL[0], ClassLoader.getSystemClassLoader());
}
private static final String ENDING_CLASS = ".class", URL_PREFIX = "file:";
public final Set<Class<?>> loadCraftbukkit(final JarFile pJar) {
if (pJar != null) {
try {
addURL(new URL(URL_PREFIX + pJar.getName()));
final Enumeration<JarEntry> entries = pJar.entries();
final Set<Class<?>> loadedClasses = new HashSet<>();
while (entries.hasMoreElements()) {
final JarEntry entry = entries.nextElement();
final String className = getClassName(entry);
if (className != null) {
try {
final Class<?> loadedClass = loadClass(className);
loadedClasses.add(loadedClass);
} catch (ClassNotFoundException | NoClassDefFoundError pExc) {
// ignore invalid class
}
}
}
return loadedClasses;
} catch (MalformedURLException | ClassFormatError ignore) {
// won't happen if legal jarfile
}
}
return null;
}
private final String getClassName(final JarEntry pEntry) {
final String name = pEntry.getName();
if (name.endsWith(ENDING_CLASS)) {
return name.substring(0, name.lastIndexOf(ENDING_CLASS)).replace('/', '.');
}
return null;
}
}
The weird thing is that out of 1800+ classes i only get for 37 classes "NoClassDefFoundError" thrown. Those classes aren't loaded and due to that my programm isn't working since Nullpointers occur.
This is the jar i am loading: http://www.file-upload.net/download-11374317/craftbukkit-1.8.8.jar.html
this is the source as far as i could find it: https://hub.spigotmc.org/stash/projects/SPIGOT/repos/craftbukkit/browse
this is the documentation as far as i could find it: https://hub.spigotmc.org/javadocs/bukkit/
How could i manage to load those 37 classes? I tried to read them in as bytes and load them my self by using the method "defineClass" of classloader but this method couldn't load them either. Does somebody know how to fix this?