my app allows third party code as plugins for certain behaviour. The actual implementation to use can be specified using a string that contains the classname. This works great.
However, I'd like the user to be able to specify the class name to use using a popup menu.
The plugin is contained in a jar that is visible in the classpath.
Is it possible for my code to find all classes implementing my plugin interface that are available at runtime? Iterating over a directory, locate the jars and check those is not really what I want. Is this sort of information dynamically available from the jre?
> my app allows third party code as plugins for certain behaviour. The > actual implementation to use can be specified using a string that > contains the classname. This works great.
> However, I'd like the user to be able to specify the class name to use > using a popup menu.
> The plugin is contained in a jar that is visible in the classpath.
> Is it possible for my code to find all classes implementing my plugin > interface that are available at runtime? Iterating over a directory, > locate the jars and check those is not really what I want. Is this > sort of information dynamically available from the jre?
> Thanks,
> bas.
The correct way to implement this type of feature is using java.util.ServiceLoader (since Java 6). This has been the recommended approach since at least Java 1.4, but ServiceLoader was added to make it easier.
On Dec 3, 3:57 am, Bas <basschu...@gmail.com> wrote:
> Hi,
> my app allows third party code as plugins for certain behaviour. The > actual implementation to use can be specified using a string that > contains the classname. This works great.
I think there are two approaches:
1/ manually scan all classes available in your jar file 2/ specify your implementation somewhere, for example in your manifest.mf file.
> However, I'd like the user to be able to specify the class name to use > using a popup menu.
> The plugin is contained in a jar that is visible in the classpath.
> Is it possible for my code to find all classes implementing my plugin > interface that are available at runtime? Iterating over a directory, > locate the jars and check those is not really what I want. Is this > sort of information dynamically available from the jre?