Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/URLClassLoader/RemoveJar.java
41149 views
1
/*
2
* Copyright (c) 2019, 2021, 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 8264048
27
*
28
* @run main/othervm RemoveJar true true testpkg.Test testpkg.Test testjar/
29
* @run main/othervm RemoveJar true true testpkg.Test testpkg.Missing testjar/
30
* @run main/othervm RemoveJar true true testpkg.Missing testpkg.Test testjar/
31
* @run main/othervm RemoveJar true true testpkg.Missing testpkg.Missing testjar/
32
*
33
* @run main/othervm RemoveJar true false testpkg.Test testpkg.Test testjar/
34
* @run main/othervm RemoveJar true false testpkg.Test testpkg.Missing testjar/
35
* @run main/othervm RemoveJar true false testpkg.Missing testpkg.Test testjar/
36
* @run main/othervm RemoveJar true false testpkg.Missing testpkg.Missing testjar/
37
*
38
* @run main/othervm RemoveJar false true testpkg.Test testpkg.Test testjar/
39
* @run main/othervm RemoveJar false true testpkg.Test testpkg.Missing testjar/
40
* @run main/othervm RemoveJar false true testpkg.Missing testpkg.Test testjar/
41
* @run main/othervm RemoveJar false true testpkg.Missing testpkg.Missing testjar/
42
*
43
* @run main/othervm RemoveJar false false testpkg.Test testpkg.Test testjar/
44
* @run main/othervm RemoveJar false false testpkg.Test testpkg.Missing testjar/
45
* @run main/othervm RemoveJar false false testpkg.Missing testpkg.Test testjar/
46
* @run main/othervm RemoveJar false false testpkg.Missing testpkg.Missing testjar/
47
*
48
* @run main/othervm RemoveJar true true testpkg.Test testpkg.Test badpath
49
*
50
* @summary URLClassLoader.close() doesn't close cached JAR file on Windows when load() fails
51
*/
52
53
import java.io.ByteArrayOutputStream;
54
import java.io.IOException;
55
import java.io.PrintStream;
56
import java.io.UncheckedIOException;
57
import java.net.URL;
58
import java.net.URLClassLoader;
59
import java.net.URLConnection;
60
import java.nio.file.Files;
61
import java.nio.file.Path;
62
import java.nio.file.Paths;
63
import java.util.Arrays;
64
import java.util.stream.Stream;
65
import java.util.zip.ZipException;
66
import java.util.spi.ToolProvider;
67
68
public class RemoveJar {
69
private final static String TEST_PKG = "testpkg";
70
private final static String JAR_DIR = "testjar/" + TEST_PKG;
71
private final static String FILE_NAME = "testjar.jar";
72
private final static ByteArrayOutputStream baos = new ByteArrayOutputStream();
73
private final static PrintStream out = new PrintStream(baos);
74
private final static ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")
75
.orElseThrow(() ->
76
new RuntimeException("jar tool not found")
77
);
78
79
private static void buildJar() throws IOException {
80
// create dir
81
mkdir(JAR_DIR);
82
// create file
83
Path path = Paths.get(JAR_DIR);
84
String src = "package " + TEST_PKG + ";\n" +
85
"class Test {}\n";
86
Files.write(Paths.get(JAR_DIR + "/Test.java"), src.getBytes());
87
// compile class
88
compile(JAR_DIR + "/Test.java");
89
// package jar
90
jar("-cf testjar.jar " + JAR_DIR);
91
}
92
93
public static void main(String args[]) throws Exception {
94
buildJar();
95
96
URLClassLoader loader = null;
97
URL url = null;
98
Path path = Paths.get(FILE_NAME);
99
100
boolean useCacheFirst = Boolean.parseBoolean(args[0]);
101
boolean useCacheSecond = Boolean.parseBoolean(args[1]);
102
String firstClass = args[2];
103
String secondClass = args[3];
104
String subPath = args[4];
105
106
try {
107
String path_str = path.toUri().toURL().toString();
108
URLConnection.setDefaultUseCaches("jar", useCacheFirst);
109
110
url = new URL("jar", "", path_str + "!/" + subPath);
111
loader = new URLClassLoader(new URL[]{url});
112
113
loader.loadClass(firstClass);
114
} catch (Exception e) {
115
System.err.println("EXCEPTION: " + e);
116
}
117
118
try {
119
URLConnection.setDefaultUseCaches("jar", useCacheSecond);
120
loader.loadClass(secondClass);
121
} catch (Exception e) {
122
System.err.println("EXCEPTION: " + e);
123
} finally {
124
loader.close();
125
Files.delete(path);
126
}
127
}
128
129
private static Stream<Path> mkpath(String... args) {
130
return Arrays.stream(args).map(d -> Paths.get(".", d.split("/")));
131
}
132
133
private static void mkdir(String cmdline) {
134
System.out.println("mkdir -p " + cmdline);
135
mkpath(cmdline.split(" +")).forEach(p -> {
136
try {
137
Files.createDirectories(p);
138
} catch (IOException x) {
139
throw new UncheckedIOException(x);
140
}
141
});
142
}
143
144
private static void jar(String cmdline) throws IOException {
145
System.out.println("jar " + cmdline);
146
baos.reset();
147
148
// the run method catches IOExceptions, we need to expose them
149
ByteArrayOutputStream baes = new ByteArrayOutputStream();
150
PrintStream err = new PrintStream(baes);
151
PrintStream saveErr = System.err;
152
System.setErr(err);
153
int rc = JAR_TOOL.run(out, err, cmdline.split(" +"));
154
System.setErr(saveErr);
155
if (rc != 0) {
156
String s = baes.toString();
157
if (s.startsWith("java.util.zip.ZipException: duplicate entry: ")) {
158
throw new ZipException(s);
159
}
160
throw new IOException(s);
161
}
162
}
163
164
/* run javac <args> */
165
private static void compile(String... args) {
166
if (com.sun.tools.javac.Main.compile(args) != 0) {
167
throw new RuntimeException("javac failed: args=" + Arrays.toString(args));
168
}
169
}
170
}
171
172
173