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.

asked Aug 25, 2015 at 13:46
5
  • this is how you load classes from JARs: stackoverflow.com/questions/460364/… Commented Aug 25, 2015 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 Commented Aug 25, 2015 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? Commented Aug 25, 2015 at 13:53
  • I have added more info on the description. Hope this helps to clarify. Commented Aug 26, 2015 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? Commented Aug 26, 2015 at 17:06

2 Answers 2

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.

answered Aug 26, 2015 at 17:08
Sign up to request clarification or add additional context in comments.

4 Comments

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.
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")
@SankhaJayalath: Not sure I'm following. The common interface Plugin needs to have the same package name as what?
@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.
0

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

answered Nov 18, 2020 at 12:38

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.