/* * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ import java.io.ByteArrayInputStream; import java.io.File; import java.io.InputStream; import java.net.URL; import java.net.URLClassLoader; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.util.jar.Attributes; import java.util.jar.Manifest; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import jdk.testlibrary.InMemoryJavaCompiler; import jdk.testlibrary.JarUtils; /* * @test * @bug 8216401 * @summary Test loading of JAR Class-Path entry with file: scheme * @library /lib/testlibrary * @build jdk.testlibrary.* JarClassPathFileEntry * * @run main/othervm JarClassPathFileEntry * @run main/othervm -Djdk.net.URLClassPath.disableClassPathURLCheck=true JarClassPathFileEntry * @run main/othervm -Djdk.net.URLClassPath.disableClassPathURLCheck=false JarClassPathFileEntry */ public class JarClassPathFileEntry { private final static boolean IS_WINDOWS = System.getProperty("os.name").startsWith("Windows"); private final static String TEST_CLASSES = System.getProperty("test.classes"); private final static String OTHER_DIR = TEST_CLASSES + "/OTHER/"; private final static Path OTHER_JAR_PATH = Paths.get(OTHER_DIR, "Other.jar"); private final static Path CONTEXT_JAR_PATH = Paths.get(TEST_CLASSES, "Context.jar"); public static void main(String[] args) throws Throwable { // Create Other.class in OTHER_DIR, off the default classpath byte klassbuf[] = InMemoryJavaCompiler.compile("Other", "public class Other {}"); writeClassToDisk("Other", klassbuf, OTHER_DIR); // Create Other.jar in OTHER_DIR JarUtils.createJarFile(OTHER_JAR_PATH, Paths.get(OTHER_DIR), Paths.get(OTHER_DIR, "Other.class")); // Create Context.class klassbuf = InMemoryJavaCompiler.compile("Context", "public class Context {}"); writeClassToDisk("Context", klassbuf, TEST_CLASSES); // Create Context.jar w/ "file:" entry for Other.jar Manifest mf = new Manifest(); Attributes attrs = mf.getMainAttributes(); attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0"); String classPathEntry = "file:" + (IS_WINDOWS ? toUnixPath(OTHER_JAR_PATH.toString()) : OTHER_JAR_PATH.toString()); attrs.put(Attributes.Name.CLASS_PATH, classPathEntry); System.out.println("Creating Context.jar with Class-Path: " + classPathEntry); JarUtils.createJarFile(CONTEXT_JAR_PATH, mf, Paths.get(TEST_CLASSES), Paths.get(TEST_CLASSES, "Context.class")); // Use URLClassLoader w/ Context.jar to load Other.class, which will // load via the Class-Path entry URL url = CONTEXT_JAR_PATH.toUri().toURL(); URLClassLoader ucl = new URLClassLoader(new URL[]{ url }, null); // don't delegate to App CL Class otherClass = Class.forName("Other", true, ucl); // ClassNotFoundException -> fail System.out.println("Loaded: " + otherClass); } /* Convert a Windows path to a unix-style path, and remove any drive letter */ private static String toUnixPath(String orig) { String retVal = new File(orig).toURI().getPath(); int colonAt = retVal.indexOf(':'); if (colonAt != -1 && colonAt < 3) { retVal = retVal.substring(colonAt + 1); // Start after the drive letter } return retVal; } private static void writeClassToDisk(String className, byte[] bytecode, String prependPath) throws Exception { writeClassToDisk(null, className, bytecode, prependPath); } private static void writeClassToDisk(ZipOutputStream zos, String className, byte[] bytecode, String prependPath) throws Exception { // Convert dotted class name to a path to a class file String pathName = className.replace('.', '/').concat(".class"); if (prependPath.length() > 0) { pathName = prependPath + "/" + pathName; } writeToDisk(zos, pathName, new ByteArrayInputStream(bytecode)); } private static void writeToDisk(ZipOutputStream zos, String pathName, InputStream is) throws Exception { if (zos != null) { ZipEntry ze = new ZipEntry(pathName); zos.putNextEntry(ze); byte[] buf = new byte[1024]; int len; while ((len = is.read(buf))>0){ zos.write(buf, 0, len); } } else { // Create the class file's package directory Path p = Paths.get(pathName); if (pathName.contains("/")) { Files.createDirectories(p.getParent()); } // Create the class file Files.copy(is, p, StandardCopyOption.REPLACE_EXISTING); } is.close(); } }