java – 为自定义应用程序加载插件的类生成NoClassDefFoundError

我有一些问题围绕着类加载的概念,我已经编程了一段时间但是我已经相对知道了类加载是如何工作的,我已经通过几个例子并阅读了详细信息在类加载和类本身后面,虽然我在一定程度上理解它是一个逃避我的概念,似乎很难用于搜索友好的术语.

基本上,我正在尝试为我为Minecraft开发的游戏模式创建’公会’,这些公会在他们自己的类中,并且在启动时或者在发出方法’reloadGuildFiles()’时加载游戏.我通过首先导出主应用程序并将其添加到正在创建的guild的类路径以及主应用程序依赖项来开发这些类.

这是’reloadGuildFiles’方法.

public void reloadGuildFiles() {
  unloadGuildFiles();

  synchronized ( _sync ) {
   System.out.println( "Loading guild class files." );

   File guildDataSourceDirectory = new File( "Prospect/Guilds/" );

   URLClassLoader urlcl = null;

   try {
    urlcl = URLClassLoader.newInstance( new URL[] { guildDataSourceDirectory.toURI().toURL() }, Thread.currentThread().getContextClassLoader() );
   } catch ( Exception e ) {
    e.printStackTrace();
    return;
   }

   if ( urlcl == null )
    return;

   for ( File guildDataFile : guildDataSourceDirectory.listFiles() ) {
    if ( !guildDataFile.getName().endsWith( ".class" ) ) {
     System.out.println( "Skipping " + guildDataFile.getName() );
     continue;
    }

    try {
     String className = guildDataFile.getName().substring( 0, guildDataFile.getName().lastIndexOf( "." ) );

     System.out.println( "Loading: " + className + "\n" +
       "\tfrom: " + guildDataFile.getPath() );

     Class<?> clazz = urlcl.loadClass( className );

     Object object = clazz.newInstance();

     if ( object instanceof Guild == false ) {
      System.out.println( "Object loaded is not an instance of Guild." );
      continue;
     }

     Guild guild = ( Guild ) object;

     if ( _guildMap.containsKey( guild.getName() ) ) {
      System.out.println( "Duplicate guild names in guild map: " + guild.getName() );
      continue;
     }

     _guildMap.put( guild.getName(), guild );
     guild.onGuildLoaded();
    } catch ( Exception e ) {
     System.out.println( e.getMessage() );
     e.printStackTrace();
     continue;
    }
   }
  }
 }
}

这是主应用程序中包含的Guild类.

public abstract class Guild {
 public abstract String getName();

 public void onGuildLoaded() {
  System.out.println( "Loaded: " + getName() );
 }
}

这是我试图加载的类

public class Warrior extends Guild {
 public String getName() {
  returns "Warrior";
 }
}

这是它给我的错误:

java.lang.NoClassDefFoundError: Guild
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(Unknown Source)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$000(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.net.FactoryURLClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at GuildManager.reloadGuildFiles(GuildManager.java:53)
        at Prospect.enable(Prospect.java:64)
        at PluginLoader.load(PluginLoader.java:205)
        at PluginLoader.reloadPlugin(PluginLoader.java:189)
        at je.d(je.java:1196)
        at je.a(je.java:430)
        at bg.a(SourceFile:24)
        at bh.a(SourceFile:218)
        at je.a(je.java:56)
        at dp.a(SourceFile:85)
        at net.minecraft.server.MinecraftServer.h(SourceFile:267)
        at net.minecraft.server.MinecraftServer.run(SourceFile:208)
        at bw.run(SourceFile:482)
Caused by: java.lang.ClassNotFoundException: Guild
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.net.FactoryURLClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 25 more

从我可以收集的内容和我理解的内容来看,即使主应用程序位于要加载的类的构建路径上,类加载器也无法识别类Guild.我想我需要尝试让类加载器识别主应用程序中包含的Guild类,无论如何要做到这一点还是有什么我明显做错了?

解决方法:

我在这里看到的唯一可能的问题是Thread.currentThread().getContextClassLoader()由于某种原因产生了一个不能用于访问Guild类的类加载器.

试试这个:

urlcl = URLClassLoader.newInstance( new URL[] { guildDataSourceDirectory.toURI().toURL() }, Guild.class.getClassLoader()); 
上一篇:【260期】Java线程池,这篇能让你和面试官聊了半小时


下一篇:java – URLClassLoader将Annotation加载为com.sun.$Proxy $27