--- old/src/jdk.jartool/share/classes/sun/tools/jar/Main.java 2017-03-14 15:10:08.000000000 +0000 +++ new/src/jdk.jartool/share/classes/sun/tools/jar/Main.java 2017-03-14 15:10:08.000000000 +0000 @@ -26,19 +26,12 @@ package sun.tools.jar; import java.io.*; -import java.lang.module.Configuration; -import java.lang.module.InvalidModuleDescriptorException; -import java.lang.module.ModuleDescriptor; +import java.lang.module.*; import java.lang.module.ModuleDescriptor.Exports; import java.lang.module.ModuleDescriptor.Provides; import java.lang.module.ModuleDescriptor.Opens; import java.lang.module.ModuleDescriptor.Requires; import java.lang.module.ModuleDescriptor.Version; -import java.lang.module.ModuleFinder; -import java.lang.module.ModuleReader; -import java.lang.module.ModuleReference; -import java.lang.module.ResolutionException; -import java.lang.module.ResolvedModule; import java.net.URI; import java.nio.ByteBuffer; import java.nio.file.Path; @@ -408,7 +401,7 @@ boolean found; if (fname != null) { try (ZipFile zf = new ZipFile(fname)) { - found = printModuleDescriptor(zf); + found = describeModule(zf); } } else { try (FileInputStream fin = new FileInputStream(FileDescriptor.in)) { @@ -604,7 +597,7 @@ int n = args.length - count; if (n > 0) { if (dflag) { - // "--print-module-descriptor/-d" does not require file argument(s) + // "--describe-module/-d" does not require file argument(s) usageError(formatMsg("error.bad.dflag", args[count])); return false; } @@ -1729,18 +1722,34 @@ .collect(joining(", ", prefix, suffix)); } - private boolean printModuleDescriptor(ZipFile zipFile) - throws IOException - { + private boolean describeModule(ZipFile zipFile) throws IOException { ZipEntry[] zes = zipFile.stream() .filter(e -> isModuleInfoEntry(e.getName())) .sorted(Validator.ENTRY_COMPARATOR) .toArray(ZipEntry[]::new); - if (zes.length == 0) - return false; - for (ZipEntry ze : zes) { - try (InputStream is = zipFile.getInputStream(ze)) { - printModuleDescriptor(is, ze.getName()); + + if (zes.length == 0) { + // No module descriptor found, derive the automatic module name + String fn = zipFile.getName(); + ModuleFinder mf = ModuleFinder.of(Paths.get(fn)); + try { + Set mref = mf.findAll(); + if (mref.size() != 1) + fatalError(formatMsg("error.expected.find.module", fn)); + String nv = mref.iterator().next().descriptor().toNameAndVersion(); + output(formatMsg("out.automodule.name", nv)); + } catch (FindException e) { + String msg = formatMsg("error.unable.derive.automodule", fn); + Throwable t = e.getCause(); + if (t != null) + msg = msg + "\n" + t.getMessage(); + output(msg); + } + } else { + for (ZipEntry ze : zes) { + try (InputStream is = zipFile.getInputStream(ze)) { + printModuleDescriptor(is, ze.getName()); + } } } return true;