How to find out what module provides concrete class?

Alan Bateman Alan.Bateman at oracle.com
Sat Dec 16 08:08:14 UTC 2023


On 15/12/2023 21:47, PavelTurk wrote:
> Hello all.
>
> Please, consider the following code:
>
> ClassLoader cl = ....
> String className = "com.foo.Bar";
> Module module = ???
>
> So, we have class name in a String and it is necessary to get 
> reference to module
> that has this class. The question - is it possible to find out:
> 1) without loading the class -
> Module module = cl.loadClass(className).getModule();
> 2) without reading content of every module
> ModuleReference reference = ...
> reference.open().list()....
>
> I mean, is there any ready solution that I can use to find module by 
> class name and without loading class?

Not in general. The class loader may know about com.sun.Bar but it may 
not be its defining loader. Even if you know that cl is (or will be) the 
defining class loader for a class named "com.sun.Bar", you don't know if 
the class is (or will be) in the unnamed module for cl or a named module 
that is mapped to cl.

-Alan


More information about the jigsaw-dev mailing list