8

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)

asked Oct 29, 2011 at 3:02
1
  • 3
    While the accepted answer is great, I would personally be very grateful for another answer that doesn't use a framework. The reason for this is (1) I want to understand how this is done and (2) there might be an instance in which I require something that JSPF doesn't provide. Commented Mar 27, 2013 at 20:20

1 Answer 1

13

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();
}
answered Oct 29, 2011 at 3:09
Sign up to request clarification or add additional context in comments.

2 Comments

@blazingkin: You don't know how to write a for-loop? I'll add one to my answer. And sorry I didn't see the last bit in your question about JSPF, but it honestly is incredibly simple to use. Writing your own plug-in infrastructure is a much larger task than suits StackOverflow questions. It involves writing custom classloaders, etc.
sorry about that your post was really helpful, for some reason it displayed as something else that just said use JSPF (did you edit it or something?)

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.