Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/tools/launcher/MainClassCantBeLoadedTest.java
41145 views
1
/*
2
* Copyright (c) 2017, 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 8174694 8181033
27
* @summary improve error message shown when main class can't be loaded
28
* @compile MainClassCantBeLoadedTest.java
29
* @run main MainClassCantBeLoadedTest
30
*/
31
32
import java.io.*;
33
import java.util.*;
34
35
public class MainClassCantBeLoadedTest extends TestHelper {
36
private MainClassCantBeLoadedTest(){}
37
38
@Test
39
void testLoadingClassWithMissingSuper() throws Exception {
40
if (!isEnglishLocale()) {
41
return;
42
}
43
44
File cwd = new File(".");
45
File srcDir = new File(cwd, "src");
46
if (srcDir.exists()) {
47
recursiveDelete(srcDir);
48
}
49
srcDir.mkdirs();
50
51
/* we want to generate two classes A and B, where B is the superclass of A
52
* class A has a main method
53
*/
54
ArrayList<String> scratchpad = new ArrayList<>();
55
scratchpad.add("public class A extends B {");
56
scratchpad.add(" public static void main(String... args) {}");
57
scratchpad.add("}");
58
createFile(new File(srcDir, "A.java"), scratchpad);
59
60
scratchpad.clear();
61
scratchpad.add("class B {}");
62
createFile(new File(srcDir, "B.java"), scratchpad);
63
64
// let's compile both
65
TestResult trCompilation = doExec(javacCmd,
66
"-d", "out",
67
new File(srcDir, "A.java").toString(),
68
new File(srcDir, "B.java").toString());
69
if (!trCompilation.isOK()) {
70
System.err.println(trCompilation);
71
throw new RuntimeException("Error: compiling");
72
}
73
74
// and now B is removed
75
File outDir = new File(cwd, "out");
76
File bClass = new File(outDir, "B.class");
77
bClass.delete();
78
79
// if A is executed
80
TestResult trExecution = doExec(javaCmd, "-cp", "out", "A");
81
// then this error message should be generated
82
trExecution.contains("Error: Could not find or load main class A");
83
trExecution.contains("Caused by: java.lang.NoClassDefFoundError: B");
84
if (!trExecution.testStatus)
85
System.err.println(trExecution);
86
}
87
88
@Test
89
void testFailToInitializeMainClass() throws Exception {
90
if (!isEnglishLocale()) {
91
return;
92
}
93
94
File cwd = new File(".");
95
File srcDir = new File(cwd, "src");
96
if (srcDir.exists()) {
97
recursiveDelete(srcDir);
98
}
99
srcDir.mkdirs();
100
101
/* we want to generate class C that will resolve additional class
102
*/
103
ArrayList<String> scratchpad = new ArrayList<>();
104
scratchpad.add("public class C {");
105
scratchpad.add(" public static void main(String... args) {");
106
scratchpad.add(" try {");
107
scratchpad.add(" System.out.println(\"loading of restricted class\");");
108
scratchpad.add(" } catch (Exception e) {");
109
scratchpad.add(" java.security.Provider p = new com.sun.crypto.provider.SunJCE();");
110
scratchpad.add(" p.toString();");
111
scratchpad.add(" }");
112
scratchpad.add(" }");
113
scratchpad.add("}");
114
createFile(new File(srcDir, "C.java"), scratchpad);
115
116
117
// Compile and execute C should succeed
118
TestResult trCompilation = doExec(javacCmd,
119
"--add-exports", "java.base/com.sun.crypto.provider=ALL-UNNAMED",
120
"-d", "out",
121
new File(srcDir, "C.java").toString());
122
if (!trCompilation.isOK()) {
123
System.err.println(trCompilation);
124
throw new RuntimeException("Error: compiling");
125
}
126
127
TestResult trExecution = doExec(javaCmd,
128
"--add-exports", "java.base/com.sun.crypto.provider=ALL-UNNAMED",
129
"-cp", "out", "C");
130
if (!trExecution.isOK()) {
131
System.err.println(trExecution);
132
throw new RuntimeException("Error: executing");
133
}
134
135
// Execute C with security manager will fail with AccessControlException
136
trExecution = doExec(javaCmd,
137
"-Djava.security.manager",
138
"--add-exports", "java.base/com.sun.crypto.provider=ALL-UNNAMED",
139
"-cp", "out", "C");
140
141
// then this error message should be generated
142
trExecution.contains("Error: Unable to initialize main class C");
143
trExecution.contains("Caused by: java.security.AccessControlException: " +
144
"access denied (\"java.lang.RuntimePermission\"" +
145
" \"accessClassInPackage.com.sun.crypto.provider\")");
146
if (!trExecution.testStatus)
147
System.err.println(trExecution);
148
}
149
150
public static void main(String[] args) throws Exception {
151
MainClassCantBeLoadedTest a = new MainClassCantBeLoadedTest();
152
a.run(args);
153
if (testExitValue > 0) {
154
System.out.println("Total of " + testExitValue + " failed");
155
throw new RuntimeException("Test failed");
156
} else {
157
System.out.println("Test passed");
158
}
159
}
160
}
161
162