1

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; } 
12
  • What is the purpose of what you're trying to accomplish? There are a few ways this might be done, and the best option will depend on your goal. Commented Dec 20, 2013 at 20:39
  • But the ideas are missing ;) is there a method to get an array of all the classes in a package? Then they could be loaded manually in a for-loop...? Commented Dec 20, 2013 at 20:43
  • Loading a Jar into UrlClassLoader should enable the loading of all classes inside the jar, have you tried to use the same classloader that already loaded the first class or have you created a new instance? It further seems that you didn't specify a valid fully qualified classname - try to replace / 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 manually Commented Dec 20, 2013 at 21:40
  • Moreover, what you are trying to achieve sounds a bit similar to one of the tasks I had for a university course a couple of years ago. Instead of a plain-text file I used the MANIFEST.MF file 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 Commented Dec 20, 2013 at 21:54
  • and you seem to load every single class of the jar from a different instance of the classloader - this further means that those types are incompatible to each other (you can't cast one object to the other) : say you load class E with instance 1 and a further class E (can be the same, even the bytes) with instance 2, then both classes will be incompatible as they got loaded with different classloaders. This holds true for a class D which has a reference (field) to class E but was loaded with a different instance of the classloader than E. Commented Dec 20, 2013 at 21:58

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.