Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get jar file name by Module in Java 9?

For example I have somefile-0.1.jar jar file which contains JPMS module. Besides, I have a reference Module somemodule to this module.

How to get jar file name by Module (using somemodule reference)? I've examined Module API but didn't find a way to do it.


1 Answers

There's no guarantee that module was loaded from a modular JAR. In any case, I think this is close to what you are looking for:

Module m = ...
ModuleLayer layer = m.getLayer();
if (layer != null) {
    ModuleReference mref = layer.configuration()
            .findModule(m.getName())
            .map(ResolvedModule::reference)
            .orElseThrow(() -> new RuntimeException("should not happen"));
}
like image 155
Alan Bateman Avatar answered Jan 06 '26 12:01

Alan Bateman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!