Re: Plugin architectures
On Mon, 5 Oct 2009, Ross wrote:
What are the best strategies for creating a Java application with a
The officially supported one.
One! You define an interface for your plugins.
Two! You package plugin implementations in JAR files, using the Service
Three! You put JARs on the classpath. Under java 6, the best way to do
this, IMHO, is with a wildcarded relative path in the JAR's Class-Path
attribute; something like:
That will add all JARs in the plugins directory adjacent to your app JAR
to the classpath.
On pre-6 javas, there are no wildcards, so the only way to add multiple
JARs is by explicitly listing them in the Class-Path, which is lame, or
putting them in the JRE's ext directory, which is lame, or by somehow
merging JARs at runtime, which is lame. Or some classloader trickery,
which is not lame, but is voodoo. You pays your money and you takes your
Four! You look the plugins up using java.util.ServiceLoader:
Unless you're on a pre-6 java, in which case you use the celebrated but
undocumented sun.misc.Service (which has been there since 1.3) - consult
javap, google, and your pineal gland for details.
Or the ServiceRegistry from ImageIO (which has been there since 1.4), even
though you're not doing image IO:
Yulava? Niob Yam!