尝试加载外部库时,URLClassLoader引发ClassNotFoundException

时间:2018-10-25 17:45:44

标签: classnotfoundexception urlclassloader

我正在尝试在检测另一个使用这些库的jar时加载外部库(导致ASM抛出ClassNotFoundException,因为它们未加载。)

我不太确定如何进行此操作,因为在此问题上我找不到在线帮助。

我在加载库时的代码如下:

        for (String path : visitor.currentBlock()) {
            if (!path.endsWith(".jar")) {
                continue;
            }

            ClassLoader classLoader = URLClassLoader.newInstance(new URL[]{file.toURI().toURL()}, getClass().getClassLoader());
            JarInputStream jis = new JarInputStream(new FileInputStream(path));

            JarEntry entry;
            while ((entry = jis.getNextJarEntry()) != null) {
                try {
                    if (entry.getName().endsWith(".class")) {
                        classLoader.loadClass(entry.getName());
                    }
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }

            jis.close();
        }

每个类都抛出此异常:

java.lang.ClassNotFoundException: XXX
at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:466)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:566)
at java.base/java.net.FactoryURLClassLoader.loadClass(URLClassLoader.java:890)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499)
at enterprises.yosemite.obfuscator.obfuscating.configuration.SimpleConfiguration.load(SimpleConfiguration.java:81)
at enterprises.yosemite.obfuscator.Bootstrap.init(Bootstrap.java:30)
at enterprises.yosemite.obfuscator.gui.Frame.main(Frame.java:24)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:473)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at 
java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:941)

0 个答案:

没有答案