Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/URLClassLoader/ClassLoad.java
38811 views
1
/*
2
* Copyright (c) 1998, 2010, 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
* @test
26
* @bug 4151665
27
* @summary Test for FileNotFoundException when loading bogus class
28
*/
29
30
import java.io.InputStream;
31
import java.io.IOException;
32
import java.net.InetSocketAddress;
33
import java.net.URL;
34
import java.net.URLClassLoader;
35
import com.sun.net.httpserver.HttpExchange;
36
import com.sun.net.httpserver.HttpHandler;
37
import com.sun.net.httpserver.HttpServer;
38
39
public class ClassLoad {
40
public static void main(String[] args) throws Exception {
41
boolean error = true;
42
43
// Start a dummy server to return 404
44
HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
45
HttpHandler handler = new HttpHandler() {
46
public void handle(HttpExchange t) throws IOException {
47
InputStream is = t.getRequestBody();
48
while (is.read() != -1);
49
t.sendResponseHeaders (404, -1);
50
t.close();
51
}
52
};
53
server.createContext("/", handler);
54
server.start();
55
56
// Client request
57
try {
58
URL url = new URL("http://localhost:" + server.getAddress().getPort());
59
String name = args.length >= 2 ? args[1] : "foo.bar.Baz";
60
ClassLoader loader = new URLClassLoader(new URL[] { url });
61
System.out.println(url);
62
Class c = loader.loadClass(name);
63
System.out.println("Loaded class \"" + c.getName() + "\".");
64
} catch (ClassNotFoundException ex) {
65
System.out.println(ex);
66
error = false;
67
} finally {
68
server.stop(0);
69
}
70
if (error)
71
throw new RuntimeException("No ClassNotFoundException generated");
72
}
73
}
74
75