diff --git a/jdk/src/java.base/share/classes/jdk/internal/jimage/ImageFileCreator.java b/jdk/src/java.base/share/classes/jdk/internal/jimage/ImageFileCreator.java index 6bb482a208f..cbdc93fdde9 100644 --- a/jdk/src/java.base/share/classes/jdk/internal/jimage/ImageFileCreator.java +++ b/jdk/src/java.base/share/classes/jdk/internal/jimage/ImageFileCreator.java @@ -139,6 +139,7 @@ public final class ImageFileCreator { } public static void recreateJimage(Path jimageFile, + String jdataName, Set archives, Map> modulePackages) throws IOException { @@ -159,12 +160,7 @@ public final class ImageFileCreator { throw new UnsupportedOperationException("Not supported, no external file " + "in a jimage file"); }, entriesForModule, order); - String fileName = jimageFile.getFileName().toString(); - if (fileName.endsWith(IMAGE_EXT)) { - fileName = fileName.substring(0, fileName.length() - - BasicImageWriter.IMAGE_EXT.length()); - } - generateJImage(jimageFile, fileName, resources, order); + generateJImage(jimageFile, jdataName, resources, order); } private void writeImage(String fileName, diff --git a/jdk/src/jdk.dev/share/classes/jdk/tools/jimage/ExtractedImage.java b/jdk/src/jdk.dev/share/classes/jdk/tools/jimage/ExtractedImage.java index cda1a682edd..05edb45a353 100644 --- a/jdk/src/jdk.dev/share/classes/jdk/tools/jimage/ExtractedImage.java +++ b/jdk/src/jdk.dev/share/classes/jdk/tools/jimage/ExtractedImage.java @@ -164,17 +164,19 @@ public final class ExtractedImage { private Set archives = new HashSet<>(); private final PrintWriter log; private final boolean verbose; - + private final String jdataName; ExtractedImage(Path dirPath, PrintWriter log, boolean verbose) throws IOException { if (!Files.isDirectory(dirPath)) { throw new IOException("Not a directory"); } + List jdataNameHolder = new ArrayList<>(); Files.walk(dirPath, 1).forEach((p) -> { try { if (!dirPath.equals(p)) { String name = getPathName(p); if (name.endsWith(ImageModuleData.META_DATA_EXTENSION)) { + jdataNameHolder.add(p.getFileName().toString()); List lines = Files.readAllLines(p); for (Entry> entry : ImageModuleDataWriter.toModulePackages(lines).entrySet()) { @@ -197,11 +199,22 @@ public final class ExtractedImage { archives = Collections.unmodifiableSet(archives); this.log = log; this.verbose = verbose; + if (jdataNameHolder.size() != 1) { + throw new IOException("Wrong module information"); + } + // The name of the metadata resource must be reused in the recreated jimage + String name = jdataNameHolder.get(0); + // Extension will be added when recreating the jimage + if (name.endsWith(ImageModuleData.META_DATA_EXTENSION)) { + name = name.substring(0, name.length() + - ImageModuleData.META_DATA_EXTENSION.length()); + } + jdataName = name; } void recreateJImage(Path path) throws IOException { - ImageFileCreator.recreateJimage(path, archives, modulePackages); + ImageFileCreator.recreateJimage(path, jdataName, archives, modulePackages); } private static String getPathName(Path path) { diff --git a/jdk/test/jdk/internal/jimage/JImageTest.java b/jdk/test/jdk/internal/jimage/JImageTest.java index 51c0bfdbf57..2e7ec735c94 100644 --- a/jdk/test/jdk/internal/jimage/JImageTest.java +++ b/jdk/test/jdk/internal/jimage/JImageTest.java @@ -66,9 +66,10 @@ public class JImageTest { String bootimage = bootimagePath.toAbsolutePath().toString(); String extractDir = Paths.get(".", "extract").toAbsolutePath().toString(); String recreateImage = Paths.get(".", "recreate.jimage").toAbsolutePath().toString(); - + String relativeRecreateImage = Paths.get(".", "recreate2.jimage").toString(); jimage("extract", "--dir", extractDir, bootimage); jimage("recreate", "--dir", extractDir, recreateImage); + jimage("recreate", "--dir", extractDir, relativeRecreateImage); System.out.println("Test successful"); } else {