1 /* 2 * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 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 25 /* 26 * @test 27 * @summary If -Djava.system.class.loader=xxx is specified in command-line, disable UseAppCDS 28 * @requires vm.cds 29 * @library /test/lib 30 * @modules java.base/jdk.internal.misc 31 * jdk.jartool/sun.tools.jar 32 * @compile test-classes/TestClassLoader.java 33 * @compile test-classes/ReportMyLoader.java 34 * @compile test-classes/TrySwitchMyLoader.java 35 * @run main SpecifySysLoaderProp 36 */ 37 38 import java.io.*; 39 import jdk.test.lib.process.OutputAnalyzer; 40 41 public class SpecifySysLoaderProp { 42 43 public static void main(String[] args) throws Exception { 44 JarBuilder.build("sysloader", "TestClassLoader", "ReportMyLoader", "TrySwitchMyLoader"); 45 46 String jarFileName = "sysloader.jar"; 47 String appJar = TestCommon.getTestJar(jarFileName); 48 TestCommon.testDump(appJar, TestCommon.list("ReportMyLoader")); 49 String warning = "VM warning: UseAppCDS is disabled because the java.system.class.loader property is specified"; 50 51 52 // (0) Baseline. Do not specify -Djava.system.class.loader 53 // The test class should be loaded from archive 54 TestCommon.run( 55 "-verbose:class", 56 "-cp", appJar, 57 "ReportMyLoader") 58 .assertNormalExit("[class,load] ReportMyLoader source: shared objects file", 59 "ReportMyLoader's loader = jdk.internal.loader.ClassLoaders$AppClassLoader@"); 60 61 // (1) Try to execute the archive with -Djava.system.class.loader=no.such.Klass, 62 // it should fail 63 TestCommon.run( 64 "-cp", appJar, 65 "-Djava.system.class.loader=no.such.Klass", 66 "ReportMyLoader") 67 .assertAbnormalExit(output -> { 68 output.shouldContain(warning); 69 output.shouldContain("ClassNotFoundException: no.such.Klass"); 70 }); 71 72 // (2) Try to execute the archive with -Djava.system.class.loader=TestClassLoader, 73 // it should run, but AppCDS should be disabled 74 TestCommon.run( 75 "-verbose:class", 76 "-cp", appJar, 77 "-Djava.system.class.loader=TestClassLoader", 78 "ReportMyLoader") 79 .assertNormalExit("ReportMyLoader's loader = jdk.internal.loader.ClassLoaders$AppClassLoader@", //<-this is still printed because TestClassLoader simply delegates to Launcher$AppLoader, but ... 80 "TestClassLoader.called = true", //<-but this proves that TestClassLoader was indeed called. 81 "TestClassLoader: loadClass(\"ReportMyLoader\",") //<- this also proves that TestClassLoader was indeed called. 82 .assertNormalExit(output -> { 83 output.shouldMatch(".class,load. TestClassLoader source: file:"); 84 output.shouldMatch(".class,load. ReportMyLoader source: file:.*" + jarFileName); 85 }); 86 87 // (3) Try to change the java.system.class.loader programmatically after 88 // the app's main method is executed. This should have no effect in terms of 89 // changing or switching the actual system class loader that's already in use. 90 TestCommon.run( 91 "-verbose:class", 92 "-cp", appJar, 93 "TrySwitchMyLoader") 94 .assertNormalExit("[class,load] ReportMyLoader source: shared objects file", 95 "TrySwitchMyLoader's loader = jdk.internal.loader.ClassLoaders$AppClassLoader@", 96 "ReportMyLoader's loader = jdk.internal.loader.ClassLoaders$AppClassLoader@", 97 "TestClassLoader.called = false"); 98 } 99 }