I programmed an interface for a very simple webserver in Java that allows the user to put his own jar-archives into a folder, and when the server starts, they will be loaded.
In that archive is a text file which defines the main class etc., but my plugin loader only loads the class defined in the file. So, when I use a second class and want to access it (Example e = new Example()
), it will result in a NoClassDefFoundError
-Exception:
Exception in thread "main" java.lang.NoClassDefFoundError: de/Ercksen/WebserverPlugin/Plugin/WasWillstDu
at de.Ercksen.WebserverPlugin.Plugin.MainPlugin.onEnable(MainPlugin.java:17)
at de.Ercksen.Webserver.Webserver.initPlugins(Webserver.java:236)
at de.Ercksen.Webserver.Webserver.loadPlugins(Webserver.java:231)
at de.Ercksen.Webserver.Webserver.execute(Webserver.java:37)
at de.Ercksen.Webserver.Webserver.main(Webserver.java:30)
Caused by: java.lang.ClassNotFoundException:
Ercksen.WebserverPlugin.Plugin.WasWillstDu
at java.net.URLClassLoader$1.run(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.lang.ClassLoader.loadClass(Unknown Source)
... 5 more
Is there a method or something that can simply load all the classes in the .jar-archive? Or any other ideas?
My class loader:
@SuppressWarnings("unchecked")
public static Class<? extends Plugin> loadPluginClass(File file, String path) throws ClassNotFoundException, ClassCastException, IOException {
URLClassLoader loader = new URLClassLoader(new URL[] {file.toURI().toURL()});
Class<?> c = loader.loadClass(path);
loader.close();
return (Class<? extends Plugin>) c;
}