我可以想到的一种了解目录中模块名称的方法是通过ModuleFinder.findAll()方法:
Path jarDirectory1, jarDirectory2;
ModuleFinder finder = ModuleFinder.of(jarDirectory1, jarDirectory2);
Set moduleReferences = finder.findAll();
Set moduleNames =
moduleReferences.stream().map(mRef -> mRef.descriptor().name()).collect(Collectors.toSet());
编辑::由艾伦(Alan)建议从罐子中取出一个模块(自动)的单层衬砌为:
String moduleName = ModuleFinder.of(path_to_jar)
.findAll().stream().findFirst()
.??map(ModuleReference:??:descriptor)
.map(Mod??uleDescriptor::name)??
.orElse(null);
另一种方法是利用ToolProvider并调整输出流以仅存储来自以下模块的名称:
Optional jar = ToolProvider.findFirst("jar");
jar.get().run(
System.out,
System.err,
"--describe-module",
"--file",
"path/to/the/jar/for/example/commons-lang3-3.6.jar"
);