Getting the automatic module name of non-modular JAR

Alan Bateman Alan.Bateman at oracle.com
Tue Apr 25 06:49:08 UTC 2017


On 24/04/2017 21:23, Gunnar Morling wrote:

> Hi,
>
> Given a non-modular JAR (e.g. represented as Path), what's the easiest
> way to obtain the automatic module name derived for this JAR?
If you just want the name then it might be more efficient to do it with 
a regular expression.

>
> I found the following:
>
>      Path nonModularJar = ...;
>      String automaticModuleName = ModuleFinder.of( nonModularJar )
>          .findAll()
>          .iterator()
>          .next()
>          .descriptor()
>          .name();
>
> Is this the best I can do?
>
> More generally speaking, is using ModuleFinder with a single path the
> only way to obtain a ModuleReference/ModuleDescriptor for a specific
> JAR?
Yes, ModuleFinder is the only way (it might be more succulent to use 
stream + findFirst but that is just detail). If you are only interested 
in the name then you could of course open the JAR file. If it contains 
module-info.class then read it with ModuleDescriptor.read, otherwise use 
a regex to derive the name.

-Alan


More information about the jigsaw-dev mailing list