7

I have been researching on how to develop an application that can load plugins. So far, I've seen that this can be done by defining an Interface, and have the plugins implement it.

However, my current issue is on how to load the plugins when they're packed in Jars. Is there a "best" way to do it?

The current logic I'm thinking of is to get each plugin and inside their Jar look for the class that implements the Interface. But I don't know how to do such lookup. I think that this logic may not be a good one, but I couldn't find any useful information on this specific topic.

**Edit1: ** Adding more information: The intended plugins would be Jar files contained inside a subdirectory where the main application's Jar would be located, like this:

Application's folder
|- Main_Application.jar
|- Plugins
  |- Plugin1.jar
  |- Plugin2.jar
  |- Steve's_plugin.jar

And so on.

What I expect is that the Application will be able to load all plugins inside the folder at runtime. So in the code, it would only be aware that the plugin's folder should exist and there should be Jars inside such folder.

Let's say I have a plugin interface like this:

interface Plugin
{
    public void run();
}

Plugins would be identified by a class that implements such interface, like so

class Plugin1 implements Plugin
{
    //attributes and other methods
    @override
    public void run()
    {
        //something happens here
    }
}

class Plugin2 implements Plugin
{
    //attributes and other methods
    @override
    public void run()
    {
        //something happens here
    }
}

The Application should be compiled only once, and be able to load any Plugins added to the folder when it is executed. For the Application to be able to load any Plugin, do I need to establish rules on the contents of the Jar, like package name and the class that implements the interface? Or it is expected that the class implementing the plugin interface could be in any package within the Jar, and have any name?

This is the more generic approach to what I would like to do with such plugins. In short, I'm planning to build an Application that will have tabs, and each plugin will provide the Interface and Functionality of each tab. I'm trying this because I want to be able to maintain each tab separately, and don't want to recompile the whole application because of changes in only one component that don't affect the others at all.

tady159
  • 73
  • 4
  • this is how you load classes from JARs: http://stackoverflow.com/questions/460364/how-to-use-classes-from-jar-files – cahen Aug 25 '15 at 13:50
  • as for the "plugins", you need to provide more information about what they really are and be more specific about the problem you're having, with code if possible – cahen Aug 25 '15 at 13:51
  • Yeah, are these plugins jars, or just a collection of class files on a path? Are they added to the default classpath, or loaded by explicitly stating the path? – Shotgun Ninja Aug 25 '15 at 13:53
  • I have added more info on the description. Hope this helps to clarify. – tady159 Aug 26 '15 at 16:52
  • Are the plugins from a secure source, i.e. you are not concerned with privileges of what the code from plugins can or cannot do? – Mifeet Aug 26 '15 at 17:06

2 Answers2

5

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.

Community
  • 1
  • 1
Aasmund Eldhuset
  • 37,289
  • 4
  • 68
  • 81
  • Thank you very much. This worked perfectly. About trusted code, I am aware of the concerns of security, and I should sandbox the plugins. But first I wanted to focus on loading the plugins first. – tady159 Aug 26 '15 at 20:54
  • For this to work you need to have the same package names for the common interface between the jars and the application. (In this example "Plugin") – Sankha Jayalath Jan 23 '23 at 14:12
  • @SankhaJayalath: Not sure I'm following. The common interface `Plugin` needs to have the same package name as what? – Aasmund Eldhuset Jan 29 '23 at 11:45
  • @AasmundEldhuset Plugin interface should be available in both the main application and external jars. The same class name Plugin won't be enough we need the Plugin class to reside in the same package name in both the main app and external jars. – Sankha Jayalath Feb 01 '23 at 05:51
0

Very old question, but still relevant if some one searches.. Adding to the accepted answer,

jfk
  • 4,335
  • 34
  • 27