Getting the automatic module name of non-modular JAR

Gunnar Morling gunnar at
Tue Apr 25 07:10:45 UTC 2017

I see; thanks, Alan.

I wanted to avoid using a regex or similar, in order to make sure the
JDK's own automatic naming rules are applied instead of
"re-implementing" them. I was kinda hoping for a method like

    Path jar = ...;
    Optional<ModuleReference> ref = ModuleReference.of( jar );

2017-04-25 8:49 GMT+02:00 Alan Bateman <Alan.Bateman at>:
> 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, otherwise use a regex to derive the
> name.
> -Alan

More information about the jigsaw-dev mailing list