0

I have a java console app that I'm writing, and I want people to be able to write plugins for it and then to distribute those plugins as jars. I want users to be able to drop a plugin (jar) into a "plugins" folder, restart the app, and have the plugin loaded and running. I don't want the user to have to specify a class/method to execute for the plugin or anything like that.

I can load the jars with a wildcard classpath to the "plugins" directory, but I need some way for those plugins to register themselves with the application by running a register() method that each plugin will need to define somewhere. How can the plugin (jar) specify where(package and class) it's register() method is defined so my app will know to call it?

I realize that OSGi can accomplish this, but this is a fairly small application and I would prefer not to use OSGi if a simpler solution exists.

Background:

These plugins register events from the app that they want to handle. The user will be able to disable the handling of specific events on a per plugin basis, so the configuration for these plugins will be stored in the app's database. When a plugin registers itself, the app will check the database to see if a configuration exists for that plugin, and if not it will create a new default configuration for it in the database.

3 Answers 3

2

Define a manifest file for your application that standardizes this behaviour. You could implement it as a property file, XML, or whatever format you chose. The manifest file would contain a standard property name, with the value being the 'startup class' for the plugin. Have the users extend this startup class from an interface or abstract class you define, so that you can enforce expected behaviour (aka, have a 'register' method).

The end user then bundles their class files and the manifest into a JAR and distributes it in the plug-in folder of your application.

OSGI is definitely a good fit for this, have you looked at Concierge? It's ridiculously small in deployed size.

3
  • I did not look at Concierge, I will take a look. For your solution tho, the manifest file you are talking about is /META-INF/MANIFEST.MF inside the jar, correct? How could I read the values from that file? Aug 5, 2011 at 21:17
  • @Bigwheels - I used the term manifest in it's more general sense, it probably would be a good idea not to use the JAR manifest file. I'll expand my answer with an example.
    – Perception
    Aug 6, 2011 at 13:21
  • Ok, what you say makes sense, just I am still unsure how I would get the values from that file into my app so I could use them Aug 7, 2011 at 5:39
0

Presumably your register method is part of a Plugin interface?

If so you could implement a custom ClassLoader to detect classes implementing this interface. Then subsequently use a Listener based approach to notifying whatever object manages your plugins of the classes' presence.

1
  • finally got around to trying this and this seems to work well, thanks :) Dec 8, 2011 at 0:38
0

There is another thread, which you may find helpful

How should I load Jars dynamically at runtime?

Also try looking into Jar class loader

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.