Classloading a plugin for a custom application produces a NoClassDefFoundError
I'm having some issues wrapping my head around the concept of classloading, I've been programming for a little while now but I'm relatively knew to how classloading works, I've gone through a couple of examples and read about the details behind classloading and classes themselves, while I understand it to a certain extent theres a concept thats escaping me and seems rather hard to put into search-friendly terms.
Essentially, I'm trying to create 'guilds' for a gamemode I've been developing for Minecraft, these guilds lie in their own classes and are loaded up with the game at startup or whenever the method 'reloadGuildFiles()' is issued. I develop these classes by first exporting the main application and adding it to the classpath of the guild being created as well as the main applications dependencies.
Here is the 'reloadGuildFiles' method.
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;
}
}
}
}
}
Here is the Guild class contained within the main application.
public abstract class Guild {
public abstract String getName();
public void onGuildLoaded() {
System.out.println( "Loaded: " + getName() );
}
}
Here is the class I am trying to classload
public class Warrior extends Guild {
public String getName() {
returns "Warrior";
}
}
Here is the error it is giving me:
java.lang.NoClassDefFoundError: Guild
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unkn开发者_StackOverflow社区own 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
From what I can gather and from what I understand, even though the main application is on the build path of the class to be loaded, the classloader does not recognize the class Guild. I think I need to try and make the classloader recognize the Guild class contained in the main application, is there anyway to do this or is there something I am clearly doing wrong?
The only possible problem I can see here is that Thread.currentThread().getContextClassLoader()
for some reason produces a classloader that can't be used to access Guild
class.
Try this instead:
urlcl = URLClassLoader.newInstance( new URL[] { guildDataSourceDirectory.toURI().toURL() }, Guild.class.getClassLoader());
精彩评论