<p>I want to create a Java program that can be extended with plugins. How can I do that and where should I look for?</p>

<p>I have a set of interfaces that the plugin must implement, and it should be in a jar. The program should watch for new jars in a relative (to the program) folder and registered them somehow.</p>

<hr>

<p>Although I do like Eclipse RCP, I think it's too much for my simple needs.</p>

<p>Same thing goes for Spring, but since I was going to look at it anyway, I might as well try it.</p>

<p>But still, I'd prefer to find a way to create my own plugin "framework" as simple as possible.</p>

[{'answer_id': 25454, 'author': 'John with waffle', 'author_id': 279, 'author_profile': 'https://Stackoverflow.com/users/279', 'pm_score': 1, 'selected': False, 'text': '<p>Have you considered building on top of Eclipse\'s Rich Client Platform, and then exposing the Eclipse extension framework?</p>\n\n<p>Also, depending on your needs, the Spring Framework might help with that and other things you might want to do:  <a href="http://www.springframework.org/" rel="nofollow noreferrer">http://www.springframework.org/</a></p>\n'}, {'answer_id': 25492, 'author': 'Steve M', 'author_id': 1693, 'author_profile': 'https://Stackoverflow.com/users/1693', 'pm_score': 7, 'selected': True, 'text': '<p>I\'ve done this for software I\'ve written in the past, it\'s very handy. I did it by first creating an Interface that all my \'plugin\' classes needed to implement. I then used the Java <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html" rel="noreferrer">ClassLoader</a> to load those classes and create instances of them.</p>\n\n<p>One way you can go about it is this:\n</p>\n\n<pre><code>File dir = new File("put path to classes you want to load here");\nURL loadPath = dir.toURI().toURL();\nURL[] classUrl = new URL[]{loadPath};\n\nClassLoader cl = new URLClassLoader(classUrl);\n\nClass loadedClass = cl.loadClass("classname"); // must be in package.class name format\n</code></pre>\n\n<p>That has loaded the class, now you need to create an instance of it, assuming the interface name is MyModule:\n</p>\n\n<pre><code>MyModule modInstance = (MyModule)loadedClass.newInstance();\n</code></pre>\n'}, {'answer_id': 25607, 'author': 'jsight', 'author_id': 1432, 'author_profile': 'https://Stackoverflow.com/users/1432', 'pm_score': 4, 'selected': False, 'text': '<p>I recommend that you take a close look at <a href="http://docs.oracle.com/javase/7/docs/technotes/guides/jar/jar.html" rel="noreferrer">the Java Service Provider (SPI) API</a>.  It provides a simple system for finding all of the classes in all Jars on the classpath that expose themselves as implementing a particular service.  I\'ve used it in the past with plugin systems with great success.</p>\n'}, {'answer_id': 26078, 'author': 'David Koelle', 'author_id': 2197, 'author_profile': 'https://Stackoverflow.com/users/2197', 'pm_score': 4, 'selected': False, 'text': '<p>Look into <a href="http://www.osgi.org" rel="nofollow noreferrer">OSGi</a>.  </p>\n\n<p>On one hand, OSGi provides all sorts of infrastructure for managing, starting, and doing lots of other things with modular software components.  On the other hand, it could be too heavy-weight for your needs.</p>\n\n<p>Incidentally, Eclipse uses OSGi to manage its plugins.</p>\n'}, {'answer_id': 152460, 'author': 'Toni Menzel', 'author_id': 1990802, 'author_profile': 'https://Stackoverflow.com/users/1990802', 'pm_score': 2, 'selected': False, 'text': '<p>At the home-grown classloader approach: \nWhile its definitely a good way to learn about classloaders there is something called "classloader hell", mostly known by people who wrestled with it when it comes to use in bigger projects. Conflicting classes are easy to introduce and hard to solve.</p>\n\n<p>And there is a good reason why eclipse made the move to OSGi years ago.\nSo, if its more then a pet project, take a serious look into OSGi. Its worth looking at.\nYou\'ll learn about classloaders PLUS an emerging technolgy standard.</p>\n'}, {'answer_id': 383779, 'author': 'Steen', 'author_id': 1448983, 'author_profile': 'https://Stackoverflow.com/users/1448983', 'pm_score': 3, 'selected': False, 'text': '<p>Although I\'ll second the accepted solution, if a basic plugin support is needed (which is the case most of the time), there is also the <a href="http://jpf.sourceforge.net/" rel="noreferrer">Java Plugin Framework</a> (JPF) which, though lacking proper documentation, is a very neat plugin framework implementation. </p>\n\n<p>It\'s easily deployable and - when you get through the classloading idiosynchrasies - very easy to develop with. A comment to the above is to be aware that plugin loadpaths below the plugin directory must be named after the <em>full</em> classpath in addition to having its class files deployed in a normal package path named path. E.g.</p>\n\n<pre><code>plugins\n`-com.my.package.plugins\n  `-com\n    `-my\n      `-package\n        `-plugins\n          |- Class1.class\n          `- Class2.class\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/25449', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2644/']