简体   繁体   English

NoClassDefFoundError。 为什么??? 我该如何解决?

[英]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.

相关问题 HiveMQ Prometheus扩展NoClassDefFoundError。 无法开始扩展 - HiveMQ Prometheus extension NoClassDefFoundError. Can't start extension NoClassDefFoundError错误。 如何设置依赖关系并部署Java应用程序? - NoClassDefFoundError. How to set dependencies and deploy a java app? 如何修复 NoClassDefFoundError: org/hibernate/HibernateException? - How can I fix NoClassDefFoundError: org/hibernate/HibernateException? 如何在 intellij 中修复此 NoClassDefFoundError? - How do I fix this NoClassDefFoundError in intellij? 如何修复错误:致命异常:java.lang.NoClassDefFoundError - How can I fix error: Fatal Exception: java.lang.NoClassDefFoundError 如何解决NoClassDefFoundError? - How can i solve a NoClassDefFoundError? 如何修复使用evosuite生成测试用例时发生的NoClassDefFoundError错误? - How can I fix the NoClassDefFoundError error that occurs when using evosuite to generate test cases? 使用 Spring 框架的 WebSocketClient 时如何修复 NoClassDefFoundError - How can I fix NoClassDefFoundError when using Spring framework's WebSocketClient 如何修复NoClassDefFoundError? - How to fix a NoClassDefFoundError? 如何修复 NoClassDefFoundError? - How to fix NoClassDefFoundError?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM