Creating a plug-in system in Java

I am trying to create a game using java and I need a plugin system for the server ...

How can I make it so that there is a folder called plugins that users can simply drop jar files there, and I can immediately call functions inside these jar files?

This implies that the community will create plugins that I donโ€™t know about (so I need it to run all plugins, even if I donโ€™t know the name of the plugin)

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

+6
source share
1 answer

Writing your own plugin infrastructure is fun, but completely unnecessary. This is a solvable problem, and you are not going to write a higher quality than the one that already exists and is proven in this area. I would say choose your battles.

I already tried JSPF and found it incredibly easy to use. And this comes from the one who did exactly what you are trying to do: I created my own plug-in infrastructure (mainly for the same purpose: dynamically load minigames) from scratch, recording the loading of classes and the framework myself. And if I did this again, I would not hesitate to think of a structure as a JSPF.

To load all classes from jars into a directory that is bound to a specific interface (say Game ), this is simple:

 PluginManager pm = PluginManagerFactory.createPluginManager(); pm.addPluginsFrom(new File("plugins/").toURI()); Collection<Game> games = new PluginManagerUtil(pm).getPlugins(Game.class); 

IIRC, the only requirement for Game performers is that it is marked with the @PluginImplementation annotation.

Edit

And then:

 for ( Game game : games ) { game.someMethod(); } 
+12
source

Source: https://habr.com/ru/post/900328/


All Articles