Reputation: 1407
I've been trying to make my own plugin system. I have 2 separate projects. One for the main 'program' that does the loading, and another project for my first plugin. The plugins implement IPlugin. I use that in the main project for my loading methods, and also in the plugin project for I can implement them for my class plugins.
I then turn the plugin project into a jar file and try to load it through the main project at runtime. The problem is this: In the main project it loads fine, but when I try to create an instance, and cast it as an IPlugin (for I can have a list of all plugins currently loaded), it says it can not be caste to IPlugin. The only reason I can think of is it doesn't recognize that the 2 IPlugin interfaces I am using (1 in each project) are the same interface. How do I 'link' an IPlugin between them? Should I put it in its own jar file and then include that in each project?
I am using BlueJ for development.
Sorry if I'm not making any sense...I am trying to explain the situation as best I can. Please let me know if you need any clarifying. Thanks!
Update:
I tried a jar file just for IPlugin, then used that JAR in both project. Now I get a 'java.lang.noClassDefFoundError: IPlugin (in java.lang.ClassLoader) It compiles fine, recognizing IPlugin etc, but at runtime It seems it doesn't know what it is...
It is happening when I am calling the URLClassLoader.loadClass() method
Upvotes: 0
Views: 380
Reputation: 74750
Your plugin project (and jar file) should not contain the IPlugin
interface (or any other classes needed by both). Instead, put the one already compiled for your main project to the compiler class path when compiling the plugin, and let the plugin classloader have the main class loader as the parent class loader.
Upvotes: 3
Reputation: 28568
You should have only one IPlugin interface. How you package it is up to you. If you put it in a separate jar that will work fine.
Upvotes: 1