Developing application with plugin support in Java

Get the list of plugin jars:

File[] jars = new File("Plugins").listFiles();

Then, use the code from this answer about loading all classes from a JAR file, but run it once for each file in jars whose name ends in ".jar". At the bottom of the loop body, after

Class c = cl.loadClass(className);

continue with

if (Plugin.class.isAssignableFrom(c)) {
    Plugin plugin = (Plugin) c.newInstance();
    // And then, do something with the plugin here
}

I share @Mifeet's concerns about security - you might want to use a SecurityManager to limit what the plugin code is allowed to do.

Tags:

Java