0

I would like to add some kind of plugin-oriented functionality to a web application I am working on. Main reason behind this is to be able to build different kind of distribution bundles (with different sets of jar files in them) for each customer.

The only thing is I don't want to change configuration files of the application to tell it where it can find its plugins. I would like the application to discover its plugins.

I guess I need something really plain and straight forward.

One possible solution I see it to try to find in classpath classes in jars (which will perform necessary setup actions) implementing some predefined interface.

Is there a way to do this? Thank you in advance.

glaz666
  • 8,707
  • 19
  • 56
  • 75
  • So are you asking about how to load extra jar's inside your jar file? But without having to tell your packing configuration about them? – Chase Oct 03 '12 at 14:57
  • @Chase, I am asking about how to load a class from jar, if I don't specify a jar in configuration. – glaz666 Oct 03 '12 at 15:15
  • [You can load a jar easily enough](http://stackoverflow.com/questions/1010919), loading just a single class would require use of very likely `java.util.zip` and a classloader. – Chase Oct 03 '12 at 15:22
  • @Chase, in application I don't know neither jar name nor classname. – glaz666 Oct 03 '12 at 15:57
  • In that case there isn't any real way. I mean you can search for the jar, but if you have no criteria find out which it is, there is no magical way to do it. My only suggestion is to look at how something like a script engine library or a look and feel gets registered when added to the classpath. – Chase Oct 03 '12 at 17:22
  • are you looking for [service provider interaces](http://www.sei.cmu.edu/library/abstracts/reports/02tn009.cfm)? – SpaceTrucker Oct 03 '12 at 20:36
  • @Chase Certainly there's a way. See my answer. – user207421 Oct 04 '12 at 00:29
  • @EJP I should of added _that I know of_. – Chase Oct 04 '12 at 01:05

1 Answers1

3

See the Javadoc for java.util.ServiceLoader.

Glenn
  • 8,932
  • 2
  • 41
  • 54
user207421
  • 305,947
  • 44
  • 307
  • 483