Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Plugin System in Java

I am attempting to make a game using java, and I need a plugin system for the server...

How might I make it so that there is a folder named plugins that the users can simply drop jar files in there and I can call functions within those jar files all at once?

This is implying that a community would make plugins that I don't know about (so i need to have it run all plugins even if I don't know the plugins name)

(I would appreciate it if I didn't need to use someone else's framework like jspf)

like image 831
blazingkin Avatar asked Oct 29 '11 03:10

blazingkin


People also ask

What is plugin 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.

How do plugins work in Java?

Java Plug-in extends the functionality of a web browser, allowing applets or Java Beans to be run under Sun's Java 2 runtime environment (JRE) rather than the Java runtime environment that comes with the web browser. Java Plug-in is part of Sun's JRE and is installed with it when the JRE is installed on a computer.

What is plugin 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.


1 Answers

Writing your own plug-in infrastructure is fun, but totally unneccesary. It's a solved problem and you're not going to write a higher quality one than one that already exists and is proven in the field. I'd say choose your battles.

I've tried out JSPF before and found it incredibly easy to use. And this coming from someone who has done exactly what you're trying to do: I've made my own plug-in infrastructure (for basically the same purpose: to load mini-games dynamically) from scratch, writing the classloading and framework myself. And if I were to do it again, I would use a framework like JSPF without hesitation.

To load all classes from jars in a directory that adhere to a certain interface (say Game), it's as easy as:

PluginManager pm = PluginManagerFactory.createPluginManager();
pm.addPluginsFrom(new File("plugins/").toURI());

Collection<Game> games = new PluginManagerUtil(pm).getPlugins(Game.class);

IIRC the only requirement on implementers of Game is that it be tagged with the @PluginImplementation annotation.

Edit

And then:

for ( Game game : games ) {
   game.someMethod();
}
like image 69
Mark Peters Avatar answered Sep 18 '22 17:09

Mark Peters