1

Is there a way to implement plugins in a Java application that is uncomplicated, can be started up when required and is well documented?

What I am looking for is something similar to OSGI, JPF and JSPF. However, OSGI is complicated, JPF isn't well documented and JSPF does not allow you to start up a plugin only when you need it.

For example, there is a plugins folder. Anyone can create a plugin by implementing an interface I supply. After the plugin is created, anyone who wants to use it can simply put it into the plugins folder of my application and the application will load it.

Or maybe, a better word for this is "Add-on".

There are some similar questions to my question, but I would like to know more. These questions have vague answers.

http://stackoverflow.com/questions/2369499/how-to-make-java-plugins?rq=1
http://stackoverflow.com/questions/7587318/how-can-i-write-my-own-plugin-in-java

If you have no idea, please tell me how to use JPF for my applications. Thanks a lot for taking your time to reading this.

EDIT: I have resolved my problem. I'm using JSPF now - it works very well for my purposes. Thank you to everyone who has tried to help and I apologize for not creating a "real question".

wei2912
  • 6,141
  • 2
  • 19
  • 20
  • 1
    Plugin for NetBeans, Eclipse or what? You must specify what you're trying to do. – Luiggi Mendoza Jul 15 '12 at 07:07
  • Reclarified. Thanks for telling me. – wei2912 Jul 15 '12 at 07:10
  • "It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical"... to me (and obviously others) it's perfectly clear and precise, would probably have had other interesting answers had it not been closed. A shame. Is there a bonus for closing questions? – RedGlyph Jul 07 '14 at 18:28

3 Answers3

5

If you start a ClassLoader for your Jar you can load or unload code at any time.

http://viralpatel.net/blogs/java-dynamic-class-loading-java-reflection-api/

http://www.javalobby.org/java/forums/t18345.html

http://zeroturnaround.com/blog/reloading-objects-classes-classloaders/

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Yup, that's what I want. I'll take a look at your links and try to code something up. Thanks. However, just a note - it seems that I have to specify the class directly, which I cannot do, or else how would it detect other plugins? – wei2912 Jul 15 '12 at 07:20
  • When you make a JAR executable, you add the name of the class to the Manifest file. You could have a file which has the class name. Before you have loaded a class you have to get its name some how. – Peter Lawrey Jul 15 '12 at 07:26
  • 1
    You can just go by convention: Jar file name equals package name equals class name (or similar scheme). – Bananeweizen Jul 15 '12 at 07:27
2

Yes you can implement them. Just for semantics sake, plugins in java are called libraries (this should make it easier for you to search on google). Normally libraries in java when built show up as .jar file

Here are some links to help you:

http://www.digilife.be/quickreferences/PT/Build%20your%20own%20Java%20library.pdf
https://stackoverflow.com/questions/3612567/how-to-create-my-own-java-libraryapi

Reply to the edit and to the comment to this post:

Then you should read this: How to design extensible software (plugin architecture)?

Community
  • 1
  • 1
fmsf
  • 36,317
  • 49
  • 147
  • 195
  • Nope, that is not what I mean. I mean something like: I have a plugins folder. I can put in plugins from any source and the program will run them on a specific event. Meanwhile, thank you for taking your time to write this. – wei2912 Jul 15 '12 at 07:12
  • The link you posted has good answers, but they're a bit complicated for a Java newbie like me. Thanks a lot. – wei2912 Jul 15 '12 at 07:19
1

This tutorial shows you how you can easily implemented plugin approach for your application but this requires you app to be Eclipse RCP powered. You will find everything you need including "plugin directory" where then will be loaded from on demand. RCP is not the only one of OSGI implementation there're others but I'd suggest you to use it because of wider community support.

I see a lot of updates to your question since the moment I started to write the answer. Seems to me you shouldn't be afraid to get deep into on of framework suggested and please choose one that looks to you complicated but proven rather than getting stacked or reinvent the bike just because you are new-joiner.

Viktor Stolbin
  • 2,899
  • 4
  • 32
  • 53