15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.nio.file.Path; 25 import java.io.BufferedReader; 26 import java.io.ByteArrayOutputStream; 27 import java.io.Closeable; 28 import java.io.File; 29 import java.io.FileFilter; 30 import java.io.FileOutputStream; 31 import java.io.IOException; 32 import java.io.InputStream; 33 import java.io.InputStreamReader; 34 import java.io.PrintStream; 35 import java.nio.charset.Charset; 36 import java.nio.file.Files; 37 import java.util.ArrayList; 38 import java.util.Arrays; 39 import java.util.Collections; 40 import java.util.List; 41 import java.util.Map; 42 import java.util.jar.JarFile; 43 import java.util.jar.JarOutputStream; 44 import java.util.jar.Pack200; 45 import java.util.zip.ZipEntry; 46 import java.util.zip.ZipFile; 47 48 import static java.nio.file.StandardCopyOption.*; 49 import static java.nio.file.StandardOpenOption.*; 50 51 /** 52 * 53 * @author ksrini 54 */ 55 56 /* 57 * This class contains the commonly used utilities. 58 */ 59 class Utils { 60 static final String JavaHome = System.getProperty("test.java", 61 System.getProperty("java.home")); 545 } 546 547 static String getJimageCmd() { 548 return getAjavaCmd("jimage"); 549 } 550 551 static String getAjavaCmd(String cmdStr) { 552 File binDir = new File(JavaHome, "bin"); 553 File unpack200File = IsWindows 554 ? new File(binDir, cmdStr + ".exe") 555 : new File(binDir, cmdStr); 556 557 String cmd = unpack200File.getAbsolutePath(); 558 if (!unpack200File.canExecute()) { 559 throw new RuntimeException("please check" + 560 cmd + " exists and is executable"); 561 } 562 return cmd; 563 } 564 565 static File createRtJar() throws IOException { 566 File libDir = new File(JavaHome, "lib"); 567 File modules = new File(libDir, "modules"); 568 List<String> cmdList = new ArrayList<>(); 569 cmdList.add(getJimageCmd()); 570 cmdList.add("extract"); 571 cmdList.add(modules.getAbsolutePath()); 572 cmdList.add("--dir"); 573 cmdList.add("out"); 574 runExec(cmdList); 575 576 File rtJar = new File("rt.jar"); 577 cmdList.clear(); 578 cmdList.add(getJarCmd()); 579 // cmdList.add("cvf"); too noisy 580 cmdList.add("cf"); 581 cmdList.add(rtJar.getName()); 582 cmdList.add("-C"); 583 cmdList.add("out"); 584 cmdList.add("."); 585 runExec(cmdList); 586 587 recursiveDelete(new File("out")); 588 return rtJar; 589 } 590 } | 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.nio.file.Path; 25 import java.io.BufferedReader; 26 import java.io.ByteArrayOutputStream; 27 import java.io.Closeable; 28 import java.io.File; 29 import java.io.FileFilter; 30 import java.io.FileOutputStream; 31 import java.io.IOException; 32 import java.io.InputStream; 33 import java.io.InputStreamReader; 34 import java.io.PrintStream; 35 import java.net.URI; 36 import java.net.URL; 37 import java.nio.charset.Charset; 38 import java.nio.file.attribute.BasicFileAttributes; 39 import java.nio.file.FileSystem; 40 import java.nio.file.FileSystems; 41 import java.nio.file.FileVisitResult; 42 import java.nio.file.FileVisitor; 43 import java.nio.file.Files; 44 import java.util.ArrayList; 45 import java.util.Arrays; 46 import java.util.Collections; 47 import java.util.List; 48 import java.util.Map; 49 import java.util.HashMap; 50 import java.util.jar.JarFile; 51 import java.util.jar.JarOutputStream; 52 import java.util.jar.Pack200; 53 import java.util.zip.ZipEntry; 54 import java.util.zip.ZipFile; 55 56 import static java.nio.file.StandardCopyOption.*; 57 import static java.nio.file.StandardOpenOption.*; 58 59 /** 60 * 61 * @author ksrini 62 */ 63 64 /* 65 * This class contains the commonly used utilities. 66 */ 67 class Utils { 68 static final String JavaHome = System.getProperty("test.java", 69 System.getProperty("java.home")); 553 } 554 555 static String getJimageCmd() { 556 return getAjavaCmd("jimage"); 557 } 558 559 static String getAjavaCmd(String cmdStr) { 560 File binDir = new File(JavaHome, "bin"); 561 File unpack200File = IsWindows 562 ? new File(binDir, cmdStr + ".exe") 563 : new File(binDir, cmdStr); 564 565 String cmd = unpack200File.getAbsolutePath(); 566 if (!unpack200File.canExecute()) { 567 throw new RuntimeException("please check" + 568 cmd + " exists and is executable"); 569 } 570 return cmd; 571 } 572 573 static File createRtJar() throws Exception { 574 File rtJar = new File("rt.jar"); 575 new JrtToZip(rtJar).run(); 576 return rtJar; 577 } 578 579 /* 580 * A helper class to create a pseudo rt.jar. 581 */ 582 static class JrtToZip { 583 584 final File outFile; 585 586 public static void main(String[] args) throws Exception { 587 new JrtToZip(new File(args[0])).run(); 588 } 589 590 JrtToZip(File outFile) throws Exception { 591 this.outFile = outFile; 592 } 593 594 void run() throws Exception { 595 URI uri = URI.create("jar:file:///" + outFile.getAbsolutePath().replace("\\", "/")); 596 Map<String, String> env = new HashMap<>(); 597 env.put("create", "true"); 598 try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) { 599 toZipfs(zipfs); 600 } 601 } 602 603 void toZipfs(FileSystem zipfs) throws Exception { 604 FileSystem jrtfs = FileSystems.getFileSystem(new URL("jrt:/").toURI()); 605 for (Path p : jrtfs.getRootDirectories()) { 606 Files.walkFileTree(p, new FileVisitor<Path>() { 607 @Override 608 public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { 609 return FileVisitResult.CONTINUE; 610 } 611 612 @Override 613 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { 614 // all we need are all the classes in the JDK 615 if (file.toString().endsWith(".class")) { 616 // System.out.println("extracting: " + file); 617 Path zpath = zipfs.getPath(file.toString()); 618 Files.createDirectories(zpath.getParent()); 619 Files.copy(file, zpath, REPLACE_EXISTING); 620 } 621 return FileVisitResult.CONTINUE; 622 } 623 624 @Override 625 public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { 626 return FileVisitResult.CONTINUE; 627 } 628 629 @Override 630 public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { 631 return FileVisitResult.CONTINUE; 632 } 633 }); 634 } 635 } 636 } 637 } |