Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to build a Plugin system with Java

People also ask

What is Java plugin interface?

The Plugin interface is used by Java's ServiceLoader to load the plugin and make it available to the rest of the app. Every plugin can choose to offer one of more factories. So, a plugin is not restricted to be of a certain “type”.

How do plugins work in Java?

Java Plug-in technology, included as part of the Java Runtime Environment, Standard Edition (Java SE), establishes a connection between popular browsers and the Java platform. This connection enables applets on Web sites to be run within a browser on the desktop.

What is plugin development in Java?

An Eclipse application consists of individual software components, called plug-ins. For example, the Eclipse Java IDE provides the functionality to develop Java applications via the JDT plug-ins. As Eclipse is build as an extensible framework, plug-ins can use and extend other plug-ins.

What is plugin based architecture?

A plug-in is a bundle that adds functionality to an application, called the host application, through some well-defined architecture for extensibility. This allows third-party developers to add functionality to an application without having access to the source code.


First you need an interface that all plugins need to implement, e.g.

public interface Plugin {
    public void load(PluginConfiguration pluginConfiguration);
    public void run();
    public void unload();
    public JComponent getConfigurationPage();
}

Plugin authors should then bundle their plugins into JAR files. Your applications opens the JAR file and could then use an attribute from JAR manifest or the list of all files in the JAR file to find the class that implements your Plugin interface. Instantiate that class, the plugin is ready to go.

Of course you may also want to implement some kind of sandboxing so that the plugin is restricted in what it can and can not do. I have created a small test application (and blogged about it) that consists of two plugins, one of which is denied access to local resources.


Use OSGi.

It is the foundation of the Eclipse plug-in system. Equinox is Eclipse's implementation (licensed EPL) and Felix is the Apache Project's implementation (licensed Apache Public License).

Eclipse provides a concrete example that OSGi can cover the points you mentioned (or you could just build your application on top of Eclipse RCP if you want a full Eclipse/SWT/JFace stack).


Since 1.6, there's been java.util.ServiceLoader which can be used if you want to code your own simple system.

But if you want anything more than basic features, use one of the existing frameworks.


Use PF4J. It has support for Web, Spring and Wicket. Easy to use and build the applications