<p>I'm writing a J2SE desktop application that requires one of its components to be pluggable. I've already defined the Java interface for this plugin. The user should be able to select at runtime (via the GUI) which implementation of this interface they want to use (e.g. in an initialisation dialog). I envisage each plugin being packaged as a JAR file containing the implementing class plus any helper classes it may require.</p>

<p>What's the best technology for doing this type of thing in a desktop Java app?</p>

[{'answer_id': 81510, 'author': 'Andrew Swan', 'author_id': 10433, 'author_profile': 'https://Stackoverflow.com/users/10433', 'pm_score': 0, 'selected': False, 'text': '<p>One approach I\'m considering is having my application start up a lightweight OSGi container, which if I understand correctly would be able to discover what plugin JAR files exist in a designated folder, which in turn would let me list them for the user to choose from. Is this feasible?</p>\n\n<p>I also found <a href="http://www.deadman.ca/articles/pluggableArticle.html" rel="nofollow noreferrer">this article</a> by Richard Deadman, but it looks a little dated (2006?) and mentions neither OSGi (at least not by name) nor the <code>java.util.jar</code> package</p>\n'}, {'answer_id': 81513, 'author': 'Philip Helger', 'author_id': 15254, 'author_profile': 'https://Stackoverflow.com/users/15254', 'pm_score': 0, 'selected': False, 'text': '<p>Did you think of using OSGi as a plugin framework? With OSGi you are able to update/replace, load or unload your modules on demand.</p>\n'}, {'answer_id': 81540, 'author': 'Olaf Kock', 'author_id': 13447, 'author_profile': 'https://Stackoverflow.com/users/13447', 'pm_score': 1, 'selected': False, 'text': '<p>If you are "just" needing one component to be pluggable, it\'s enough to simply instantiate the classes based on meta information, e.g. read via a classloaders META-INF/ information from the various jars that are on your classpath or in a certain plugin directory.</p>\n\n<p>OSGi on the other hand provides means to structure your whole application. If you already have a large Desktop application that needs one part pluggable, this would be a steep learning curve. If you start blank with what will be a Desktop app, OSGi provides means to modularizing the whole application. It\'s about "isolation of components" and independence of modules. </p>\n\n<p>Apache Felix provides a nice start if you want to go down OSGi lane. It might look complicated and heavyweight, but that\'s only because one is not used to that level of isolation between modules. It used to be so easy to just call any public method...</p>\n'}, {'answer_id': 81558, 'author': 'Garth Gilmour', 'author_id': 2635682, 'author_profile': 'https://Stackoverflow.com/users/2635682', 'pm_score': 2, 'selected': False, 'text': "<p>OSGI is certainly a valid way to go. But, assuming you dont need to unload to reload the plugin, it might be using a hammer to crack a nut. </p>\n\n<p>You could use the classes in 'java.util.jar' to scan each JAR file in your plugins folder and then use a 'java.net.URLClassLoader' to load in the correct one.</p>\n"}, {'answer_id': 82108, 'author': 'Riduidel', 'author_id': 15619, 'author_profile': 'https://Stackoverflow.com/users/15619', 'pm_score': 4, 'selected': True, 'text': '<p>After many tries for plugin-based Java architectures (what is precisely what you seem to look for), I finally found <a href="http://code.google.com/p/jspf/" rel="noreferrer">JSPF</a> to be the best solution for Java5 code. it do not have the huge needs of OSGI like solutions, but is instead rather easy to use.</p>\n'}]
['https://Stackoverflow.com/questions/81495', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/10433/']