What is the optimal way to get all files in META-INF/services/ in automatic module
Alan Bateman
alan.bateman at oracle.com
Tue Dec 24 06:54:54 UTC 2024
On 23/12/2024 09:12, PavelTurk wrote:
> Hello everyone,
>
> I have `Module module`. If this module is automatic I need to get the list of all its services/providers, so,
>
> as I understand the only way to do it is to read its META-INF/services folder. I tried:
>
>
> if (module.getDescriptor().isAutomatic()) {
> ClassLoader classLoader = module.getClassLoader();
> Enumeration<URL> resources = classLoader.getResources("META-INF/services/");
>
> while (resources.hasMoreElements()) {
> URL resource = resources.nextElement();
> ...
> }
> }
>
> but it doesn't work. All `resource`s have same path ending in `....META-INF/service`.
>
> I tried:
>
> ModuleReference reference = ... ;
> var entries = reference.open().list().collect(Collectors.toList());
>
> It worked, but I need only one folder, but not all with all classes etc. Could anyone what is the optimal way to do it?
In your second attempt then you course add a filter it to just the
resources with names that start with META-INF/services.
In any case, I assume the method you are looking for is
ModuleDescriptor::provides. It returns the set of Provides objects for
service providers in the module (it doesn't matter than the module is an
automatic module).
-Alan.
More information about the jigsaw-dev
mailing list