Path: blob/master/test/jdk/java/net/URLClassLoader/ClassLoad.java
41149 views
/*1* Copyright (c) 1998, 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*/2223/**24* @test25* @bug 415166526* @modules jdk.httpserver27* @library /test/lib28* @summary Test for FileNotFoundException when loading bogus class29*/3031import java.io.InputStream;32import java.io.IOException;33import java.net.InetAddress;34import java.net.InetSocketAddress;35import java.net.URL;36import java.net.URLClassLoader;37import com.sun.net.httpserver.HttpExchange;38import com.sun.net.httpserver.HttpHandler;39import com.sun.net.httpserver.HttpServer;40import jdk.test.lib.net.URIBuilder;4142public class ClassLoad {43public static void main(String[] args) throws Exception {44boolean error = true;4546// Start a dummy server to return 40447HttpServer server = HttpServer.create();48server.bind(new InetSocketAddress(49InetAddress.getLoopbackAddress(), 0), 0);50HttpHandler handler = new HttpHandler() {51public void handle(HttpExchange t) throws IOException {52InputStream is = t.getRequestBody();53while (is.read() != -1);54t.sendResponseHeaders (404, -1);55t.close();56}57};58server.createContext("/", handler);59server.start();6061// Client request62try {63URL url = URIBuilder.newBuilder()64.scheme("http")65.loopback()66.port(server.getAddress().getPort())67.toURL();68String name = args.length >= 2 ? args[1] : "foo.bar.Baz";69ClassLoader loader = new URLClassLoader(new URL[] { url });70System.out.println(url);71Class c = loader.loadClass(name);72System.out.println("Loaded class \"" + c.getName() + "\".");73} catch (ClassNotFoundException ex) {74System.out.println(ex);75error = false;76} finally {77server.stop(0);78}79if (error)80throw new RuntimeException("No ClassNotFoundException generated");81}82}838485