简体   繁体   中英

NoClassDefFoundError only for few classes when loading classes from external jar

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?

It is hard to know without seeing the error cause.

Usually this is caused by having dependencies not included in the classpath (a ClassNotFoundException that results from a class loader failing to load a superclass is wrapped in a NoClassDefFoundError ) or some runtime exception in a static field or block.

Worth to point this excellent explanation of NoClassDefFoundError : https://stackoverflow.com/a/5756989/4483113

It seems that your app reach the limitation of 65K methods, which is quite famous limitation of Android. source: https://developer.android.com/tools/building/multidex.html#dev-build

for additional info please see: java.lang.NoClassDefFoundError exception on some devices

java.lang.NoClassDefFoundError and java.lang.NoSuchMethodError are thrown at runtime when they are accessed and the classloader is not able to load the classes because it's not present in classpath. If your program never tries to access a class or method at runtime which is not present in the classpath, you'll never get a NoClassDefFoundError or NoSuchMethodError .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM