1 /*
   2  * Copyright (c) 2019, 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 import java.net.URL;
  25 import java.net.URLClassLoader;
  26 import java.nio.file.Path;
  27 import java.nio.file.Paths;
  28 
  29 /*
  30  * @test
  31  * @bug 8231924 8233091 8233272
  32  * @summary Confirm load (but not link) behavior of Class.forName()
  33  * @library /test/lib
  34  *
  35  * @compile MissingClass.java
  36  * @compile Container.java
  37  *
  38  * @run main/othervm ClassFileInstaller -jar classes.jar Container Container$1
  39  *
  40  * @run main/othervm NonLinking init
  41  * @run main/othervm NonLinking load
  42  */
  43 /*
  44  * The @compile and '@main ClassFileInstaller' tasks above create a classes.jar
  45  * file containing the .class file for Container, but not MissingClass.
  46  */
  47 
  48 public class NonLinking {    
  49     public static void main(String[] args) throws Throwable {
  50         Path jarPath = Paths.get("classes.jar");
  51         URL url = jarPath.toUri().toURL();
  52         URLClassLoader ucl1 = new URLClassLoader("UCL1",
  53                                                  new URL[] { url },
  54                                                  null); // Don't delegate
  55         switch(args[0]) {
  56             case "init":
  57                 try {
  58                     // Trying to initialize Container without MissingClass -> NCDFE
  59                     Class.forName("Container", true, ucl1);
  60                     throw new RuntimeException("Missed expected NoClassDefFoundError");
  61                 } catch (NoClassDefFoundError expected) {}
  62                 break;
  63             case "load":
  64                 // Loading (but not linking) Container will succeed.
  65                 // Before 8233091, this fails with NCDFE due to linking.
  66                 Class.forName("Container", false, ucl1);
  67                 break;
  68             default:
  69                 throw new RuntimeException("Unknown command: " + args[0]);
  70         }
  71     }
  72 }