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; }
/with.. If this still does not solve your problem you might try to iterate through the content of the jar file and extract the classnames manuallyMANIFEST.MFfile inside the jar to specify the plugin class which should be loaded (and all its dependencies). Maybe there is something useful for you in there. PS: OK, had a second look on your code - the fully qualified classname should not be the problem, but maybe the closing of the classloader - at this point it has not instantiated the class or loaded its dependencies