英文:
java.lang.NoClassDefFoundError in Plugin System
问题
这是我的类加载器:
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) {
// 第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;
/**
* 作者:mnjg123
*
*/
public class PayLoad extends Feature {
/**
* @param name
*/
public PayLoad() {
super("test", false);
}
@Override
public void onEnable() {
System.out.println("true");
super.onEnable();
}
}
我做错了什么?我该如何改进它?
英文:
So, i have an program, that i want to expand with plugins or modules.
I load them out of the folder and get a ClassLoader to loop through all files, to see if they have the Super class of my Software
Then i get this exception:
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]
This is my ClassLoader:
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;
}
and this is the Class i want to load
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();
}
}
What do i do wrong? And how can i improve it?
答案1
得分: 0
一般来说,NoClassDefFoundError 是指在构建过程中缺少某些组件,导致在使用时抛出 NoClassDefFoundError。
显然,该路径下找不到文件:
me/mnjg123/privacy1/de/privacy2/system/core/objects/Feature
解决方法是重新进行构建。
希望这能对您有所帮助。
英文:
Generally speaking, NoClassDefFoundError refers to the lack of some components when building, which results in a NoClassDefFoundError thrown when using.
Obviously,File not found under this path:
me/mnjg123/privacy1/de/privacy2/system/core/objects/Feature
The solution is to rebuild once
Hope this helps you.
专注分享java语言的经验与见解,让所有开发者获益!
评论