Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/misc/URLClassPath/JarClassPathFileEntry.java
38838 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.File;24import java.net.URL;25import java.net.URLClassLoader;26import java.nio.file.Path;27import java.nio.file.Paths;28import java.util.jar.Attributes;29import java.util.jar.Manifest;30import jdk.testlibrary.InMemoryJavaCompiler;31import jdk.testlibrary.JarUtils;3233/*34* @test35* @bug 821640136* @summary Test loading of JAR Class-Path entry with file: scheme37* @library /lib/testlibrary38*39* @run main/othervm JarClassPathFileEntry40* @run main/othervm -Djdk.net.URLClassPath.disableClassPathURLCheck=true JarClassPathFileEntry41* @run main/othervm -Djdk.net.URLClassPath.disableClassPathURLCheck=false JarClassPathFileEntry42*/4344public class JarClassPathFileEntry {45private final static boolean IS_WINDOWS = System.getProperty("os.name").startsWith("Windows");4647private final static String TEST_CLASSES = System.getProperty("test.classes");48private final static String OTHER_DIR = TEST_CLASSES + "/OTHER/";4950private final static Path OTHER_JAR_PATH = Paths.get(OTHER_DIR, "Other.jar");51private final static Path CONTEXT_JAR_PATH = Paths.get(TEST_CLASSES, "Context.jar");5253public static void main(String[] args) throws Throwable {54// Create Other.class in OTHER_DIR, off the default classpath55byte klassbuf[] = InMemoryJavaCompiler.compile("Other",56"public class Other {}");57ClassFileInstaller.writeClassToDisk("Other", klassbuf, OTHER_DIR);5859// Create Other.jar in OTHER_DIR60JarUtils.createJarFile(OTHER_JAR_PATH,61Paths.get(OTHER_DIR),62Paths.get(OTHER_DIR, "Other.class"));6364// Create Context.class65klassbuf = InMemoryJavaCompiler.compile("Context",66"public class Context {}");67ClassFileInstaller.writeClassToDisk("Context", klassbuf, TEST_CLASSES);6869// Create Context.jar w/ "file:" entry for Other.jar70Manifest mf = new Manifest();71Attributes attrs = mf.getMainAttributes();72attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0");7374String classPathEntry = "file:" + (IS_WINDOWS ? toUnixPath(OTHER_JAR_PATH.toString())75: OTHER_JAR_PATH.toString());76attrs.put(Attributes.Name.CLASS_PATH, classPathEntry);7778System.out.println("Creating Context.jar with Class-Path: " + classPathEntry);79JarUtils.createJarFile(CONTEXT_JAR_PATH, mf,80Paths.get(TEST_CLASSES),81Paths.get(TEST_CLASSES, "Context.class"));8283// Use URLClassLoader w/ Context.jar to load Other.class, which will84// load via the Class-Path entry85URL url = CONTEXT_JAR_PATH.toUri().toURL();86URLClassLoader ucl = new URLClassLoader(new URL[]{ url },87null); // don't delegate to App CL88Class<?> otherClass = Class.forName("Other", true, ucl); // ClassNotFoundException -> fail89System.out.println("Loaded: " + otherClass);90}9192/* Convert a Windows path to a unix-style path, and remove any drive letter */93private static String toUnixPath(String orig) {94String retVal = new File(orig).toURI().getPath();95int colonAt = retVal.indexOf(':');9697if (colonAt != -1 && colonAt < 3) {98retVal = retVal.substring(colonAt + 1); // Start after the drive letter99}100return retVal;101}102}103104105