我在类加载的概念上遇到了一些问题,我已经编程了一段时间,但我对类加载的工作原理相对了解,我已经浏览了几个示例并阅读了详细信息在类加载和类本身的背后,虽然我在一定程度上理解它,但有一个概念让我无法理解,而且似乎很难用搜索友好的术语来表达。
本质上,我正在尝试为我一直在为 Minecraft 开发的游戏模式创建“公会”,这些公会位于它们自己的类中,并在启动时或每当发出“reloadGuildFiles()”方法时加载游戏。我通过首先导出主应用程序并将其添加到正在创建的公会的类路径以及主应用程序依赖项来开发这些类。
这是'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 类,无论如何要这样做还是我明显做错了什么?