Dynamic .jar Loading

So recently I’ve been trying to design a modular system for plugin, where modules are automatically download and enabled on start up. Downloading, searching, updating, etc. all works perfectly, the problems occur when I begin to load the modules. I’m using this class to load them, and I’ve tested it on a clean Java run, and it would like a charm, however, when I use the class on Sponge (vanilla and forge), it fails - sort of. After loading the class, I iterate through them, check if they have a certain annotation, get their constructors, and initialize them. This process fails when I call getDeclaredConstructors, and throws a ClassDefNotFound exception, despite it being loaded, and me checking if it has a certain annotation. I’m assuming this has to do with Forge’s and Sponge’s debauchery with the ClassLoaders, and if it does, is it a bug in sponge, a design flaw, a bug in my application, or an impassable barrier?

Any help is greatly appreciated, thanks :slightly_smiling:

If you touch classloading in minecraft then you 100% need to understand what you’re getting yourself into.



Well geez…

Is there maybe some alternate way I could do this?
Would be really useful if sponge could dynamically load libraries… I guess that’s wishful thinking.

For this case it seems that Liteloader can also inject fml as an injected tweaker.
Of course vice versa.

Is it possible to read the plugin metadata before the jar is fully loaded into the class path? If so you could have a system where plugins could elect to be disabled before being loaded, but then you have an entirely different issue of how to do the init event cycle since they are being loaded late…

Then you could just package your optional libraries as plugins and somehow load them when needed?

Through complete and total hackery, I’ve managed to inject my library into the classpath. Reflected into the registered ClassLoader and added a URL… It’s a temporary solution, but It’ll do for now.