gpt4 book ai didi

插件系统中的java.lang.NoClassDefFoundError

转载 作者:行者123 更新时间:2023-12-01 17:30:24 24 4
gpt4 key购买 nike

所以,我有一个程序,我想用插件或模块扩展它。

我将它们从文件夹中加载出来,并使用 ClassLoader 循环遍历所有文件,以查看它们是否具有我的软件的 super 类

然后我得到这个异常:

 java.lang.NoClassDefFoundError: me/mnjg123/privacy1/de/privacy2/system/core/objects/Feature
at java.lang.ClassLoader.defineClass1(Native Method) ~[?:1.8.0_242]
at java.lang.ClassLoader.defineClass(ClassLoader.java:757) ~[?:1.8.0_242]
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) ~[?:1.8.0_242]
at java.net.URLClassLoader.defineClass(URLClassLoader.java:468) ~[?:1.8.0_242]
at java.net.URLClassLoader.access$100(URLClassLoader.java:74) ~[?:1.8.0_242]
at java.net.URLClassLoader$1.run(URLClassLoader.java:369) ~[?:1.8.0_242]
at java.net.URLClassLoader$1.run(URLClassLoader.java:363) ~[?:1.8.0_242]
at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_242]
at java.net.URLClassLoader.findClass(URLClassLoader.java:362) ~[?:1.8.0_242]
at java.lang.ClassLoader.loadClass(ClassLoader.java:419) ~[?:1.8.0_242]
at java.lang.ClassLoader.loadClass(ClassLoader.java:352) ~[?:1.8.0_242]
at me.mnjg123.ragemade.de.bungee.system.core.handlers.FeatureHandler.loadFeatures(FeatureHandler.java:88) ~[?:?]
at me.mnjg123.privacy1.de.privacy2.system.core.privacy3.addFeature(privacy3.java:40) ~[?:?]
at me.mnjg123.privacy1.de.privacy2.system.core.privacy3.onEnable(privacy3.java:33) ~[?:?]
Caused by: java.lang.ClassNotFoundException: me.mnjg123.privacy1.de.privacy2.system.core.objects.Feature
at java.net.URLClassLoader.findClass(URLClassLoader.java:382) ~[?:1.8.0_242]
at java.lang.ClassLoader.loadClass(ClassLoader.java:419) ~[?:1.8.0_242]
at java.lang.ClassLoader.loadClass(ClassLoader.java:352) ~[?:1.8.0_242]

这是我的类加载器:

public List<Feature> loadFeatures() throws FeatureExeption {

List<Feature> features = new ArrayList<>();

try {

String path = new File("").getAbsolutePath();
File folder = new File(path + "/plugins/privacy3/addons/");
if(!folder.exists())
folder.mkdirs();
File[] files = folder.listFiles();
if (files != null) {
for (File plugin : files) {
ZipInputStream zip = new ZipInputStream(new FileInputStream(plugin));
List<String> classes = new ArrayList<>();

for(ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry()) {
if(!entry.isDirectory() && entry.getName().endsWith(".class")) {
String className = entry.getName().replace("/", ".");

classes.add(className.substring(0, className.length() - ".class".length()));
}
}

URLClassLoader classloader = new URLClassLoader(new URL[]{plugin.toURI().toURL()});


for(String className : classes) {


line 88 -> Class<?> cl = classloader.loadClass(className);

}

zip.close();
classloader.close();
}
}

} catch (IOException | ClassNotFoundException e) {

e.printStackTrace();
}

return features;

}

这是我想要加载的类

package me.mnjg123.featuretest;

import me.mnjg123.privacy1.de.privacy2.system.core.objects.Feature;

/**
* @author mnjg123
*
*/
public class PayLoad extends Feature {




/**
* @param name
*/
public PayLoad() {

super("test", false);

}

@Override
public void onEnable() {
System.out.println("true");

super.onEnable();
}



}

我做错了什么?我该如何改进它?

最佳答案

一般来说,NoClassDefFoundError是指构建时缺少某些组件,导致使用时抛出NoClassDefFoundError。

显然,该路径下没有找到文件:我/mnjg123/privacy1/de/privacy2/system/core/objects/Feature

解决方案是重建一次

希望这对您有帮助。

关于插件系统中的java.lang.NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61132363/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com