< prev index next >

test/tools/pack200/Utils.java

Print this page

        

@@ -30,17 +30,25 @@
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.PrintStream;
+import java.net.URI;
+import java.net.URL;
 import java.nio.charset.Charset;
+import java.nio.file.attribute.BasicFileAttributes;
+import java.nio.file.FileSystem;
+import java.nio.file.FileSystems;
+import java.nio.file.FileVisitResult;
+import java.nio.file.FileVisitor;
 import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
+import java.util.HashMap;
 import java.util.jar.JarFile;
 import java.util.jar.JarOutputStream;
 import java.util.jar.Pack200;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipFile;

@@ -560,31 +568,70 @@
                     cmd + " exists and is executable");
         }
         return cmd;
     }
 
-    static File createRtJar() throws IOException {
-        File libDir = new File(JavaHome, "lib");
-        File modules = new File(libDir, "modules");
-        List<String> cmdList = new ArrayList<>();
-        cmdList.add(getJimageCmd());
-        cmdList.add("extract");
-        cmdList.add(modules.getAbsolutePath());
-        cmdList.add("--dir");
-        cmdList.add("out");
-        runExec(cmdList);
-
+    static File createRtJar() throws Exception {
         File rtJar = new File("rt.jar");
-        cmdList.clear();
-        cmdList.add(getJarCmd());
-        // cmdList.add("cvf"); too noisy
-        cmdList.add("cf");
-        cmdList.add(rtJar.getName());
-        cmdList.add("-C");
-        cmdList.add("out");
-        cmdList.add(".");
-        runExec(cmdList);
-
-        recursiveDelete(new File("out"));
+        new JrtToZip(rtJar).run();
         return rtJar;
     }
+
+    /*
+     * A helper class to create a pseudo rt.jar.
+     */
+    static class JrtToZip {
+
+        final File outFile;
+
+        public static void main(String[] args) throws Exception {
+            new JrtToZip(new File(args[0])).run();
+        }
+
+        JrtToZip(File outFile) throws Exception {
+            this.outFile = outFile;
+        }
+
+        void run() throws Exception {
+            URI uri = URI.create("jar:file:///" + outFile.getAbsolutePath().replace("\\", "/"));
+            Map<String, String> env = new HashMap<>();
+            env.put("create", "true");
+            try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {
+                toZipfs(zipfs);
+            }
+        }
+
+        void toZipfs(FileSystem zipfs) throws Exception {
+            FileSystem jrtfs = FileSystems.getFileSystem(new URL("jrt:/").toURI());
+            for (Path p : jrtfs.getRootDirectories()) {
+                Files.walkFileTree(p, new FileVisitor<Path>() {
+                    @Override
+                    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
+                        return FileVisitResult.CONTINUE;
+                    }
+
+                    @Override
+                    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
+                        // all we need are all the classes in the JDK
+                        if (file.toString().endsWith(".class")) {
+                            // System.out.println("extracting: " + file);
+                            Path zpath = zipfs.getPath(file.toString());
+                            Files.createDirectories(zpath.getParent());
+                            Files.copy(file, zpath, REPLACE_EXISTING);
+                        }
+                        return FileVisitResult.CONTINUE;
+                    }
+
+                    @Override
+                    public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
+                        return FileVisitResult.CONTINUE;
+                    }
+
+                    @Override
+                    public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
+                        return FileVisitResult.CONTINUE;
+                    }
+                });
+            }
+        }
+    }
 }
< prev index next >