[英]NoClassDefFoundError. Why??? How can I fix it?
I wrote my classloader:我写了我的类加载器:
package ru.sberbank.school.homework8;
import ru.sberbank.school.homework8.plugin.Plugin;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class PluginManager extends ClassLoader {
private final String pluginRootDirectory;
public PluginManager(String pluginRootDirectory) {
this.pluginRootDirectory = pluginRootDirectory;
}
public Plugin load(String pluginName, String pluginClassName) {
String name = pluginName + "." + pluginClassName;
try {
Class clazz;
try {
clazz = super.findSystemClass(name);
} catch (ClassNotFoundException e) {
String fileName = pluginRootDirectory + "\\" + pluginName + "\\" + pluginClassName + ".class";
try (FileInputStream fin = new FileInputStream(fileName)) {
byte[] buffer = new byte[(int) (new File(fileName).length())];
fin.read(buffer);
clazz = defineClass(name, buffer, 0, buffer.length);
}
}
return (Plugin)clazz.newInstance();
} catch (IOException | InstantiationException | IllegalAccessException ignored) {
return null;
}
}
} }
When I run it:当我运行它时:
package ru.sberbank.school.homework8;
import ru.sberbank.school.homework8.plugin.Plugin;
public class PluginManagerTest {
public static void main(String[] args) {
String pluginRootDirectory = "D:\\sbt\\target\\classes\\ru\\sberbank\\school\\homework8";
PluginManager pluginManager = new PluginManager(pluginRootDirectory);
Plugin plugin = pluginManager.load("plugin", "PluginImpl");
if (plugin != null) {
plugin.doUseful();
}
}
}
Exception in thread "main" java.lang.NoClassDefFoundError: plugin/PluginImpl (wrong name: ru/sberbank/school/homework8/plugin/PluginImpl) at java.lang.ClassLoader.defineClass1(Native Method)线程“main”中的异常 java.lang.NoClassDefFoundError: plugin/PluginImpl(错误名称:ru/sberbank/school/homework8/plugin/PluginImpl)在 java.lang.ClassLoader.defineClass1(Native Method)
I get NoClassDefFoundError.我得到 NoClassDefFoundError。 Why???为什么??? How can I fix it???我该如何修复它???
Help me, please!请帮帮我!
package ru.sberbank.school.homework8.plugin;
public class PluginImpl implements Plugin {
@Override
public void doUseful() {
System.out.println("My plugin!");
}
}
You get this error because you don't provide the correct FQN of your class, indeed in your load
method, you try to find the class corresponding to pluginName + "." + pluginClassName
你得到这个错误是因为你没有提供你的类的正确 FQN,确实在你的load
方法中,你试图找到对应于pluginName + "." + pluginClassName
的类pluginName + "." + pluginClassName
pluginName + "." + pluginClassName
that will be in your case plugin.PluginImpl
but the package name of your class PluginImpl
is actually ru.sberbank.school.homework8.plugin
such that the real FQN of your class is ru.sberbank.school.homework8.plugin.PluginImpl
. pluginName + "." + pluginClassName
在你的情况下是plugin.PluginImpl
但你的类PluginImpl
的包名实际上是ru.sberbank.school.homework8.plugin
这样你的类的真正 FQN 是ru.sberbank.school.homework8.plugin.PluginImpl
.
To fix this problem, you need to replace:要解决此问题,您需要更换:
Plugin plugin = pluginManager.load("plugin", "PluginImpl");
With:和:
Plugin plugin = pluginManager.load("ru.sberbank.school.homework8.plugin", "PluginImpl");
Or you could modify your method load
to add a prefix assuming that you will always retrieve your plugins from the same root package:或者你可以修改你的方法load
来添加一个前缀,假设你总是从同一个根包中检索你的插件:
public Plugin load(String pluginName, String pluginClassName) {
String name = "ru.sberbank.school.homework8." + pluginName + "." + pluginClassName;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.