ClassLoader.getResources vs Module.getResourceAsStream
Alan Bateman
Alan.Bateman at oracle.com
Mon Jul 16 10:47:19 UTC 2018
On 16/07/2018 11:06, Stephen Colebourne wrote:
> In my experience (as I've written before), ClassLoader.getResources is
> perhaps the biggest pain point I've experienced in trying to move
> beyond Java 8. The method seems to have been very widely used, and IMO
> was considered to be preferred over Class.getResourceXxx. And it is
> very confusing to use once modules come into play.
Class.getResourceXXX is the better choice when locating ones own
resources, ClassLoader.getResourceXX is for searching for a resource in
other components, the ClassLoader provide some context on where to
search. There are of course libraries that were using ClassLoader to
locate their own resources and those cases may need some changes in the
event that the code is migrated to an explicit module and the resources
are encapsulated. If they aren't encapsulated then the existing code
using the ClassLoader methods will work of course.
>
> There is no simple replacement for the ability to search for resources
> across jar files.
The ClassLoader resources and getResources method work as before and
will locate resources in modular JARs when the resources are not
encapsulated.
> Module.getResourceXxx cannot be used as it only
> returns one resource from the unamed module, when there could be many
> (and the docs are no particularly clear on what they do). This is very
> inconvenient.
Module.getResourceXXX works with named modules too.
-Alan
More information about the jigsaw-dev
mailing list