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