8181033: Confusing message: A JNI error has occurred, please check your installation and try again

Reviewed-by: alanb, dholmes, ksrini
This commit is contained in:
Mandy Chung 2017-05-25 10:40:08 -07:00
parent aea187fdd2
commit 4f8f7b2501
3 changed files with 81 additions and 6 deletions

View File

@ -568,7 +568,6 @@ public final class LauncherHelper {
}
validateMainClass(mainClass);
return mainClass;
}
@ -619,7 +618,7 @@ public final class LauncherHelper {
}
} catch (LinkageError le) {
abort(null, "java.launcher.module.error3", mainClass, m.getName(),
le.getClass().getName() + ": " + le.getLocalizedMessage());
le.getClass().getName() + ": " + le.getLocalizedMessage());
}
if (c == null) {
abort(null, "java.launcher.module.error2", mainClass, mainModule);
@ -707,14 +706,22 @@ public final class LauncherHelper {
// Check the existence and signature of main and abort if incorrect
static void validateMainClass(Class<?> mainClass) {
Method mainMethod;
Method mainMethod = null;
try {
mainMethod = mainClass.getMethod("main", String[].class);
} catch (NoSuchMethodException nsme) {
// invalid main or not FX application, abort with an error
abort(null, "java.launcher.cls.error4", mainClass.getName(),
JAVAFX_APPLICATION_CLASS_NAME);
return; // Avoid compiler issues
} catch (Throwable e) {
if (mainClass.getModule().isNamed()) {
abort(e, "java.launcher.module.error5",
mainClass.getName(), mainClass.getModule(),
e.getClass().getName(), e.getLocalizedMessage());
} else {
abort(e,"java.launcher.cls.error7", mainClass.getName(),
e.getClass().getName(), e.getLocalizedMessage());
}
}
/*

View File

@ -208,6 +208,9 @@ java.launcher.cls.error5=\
java.launcher.cls.error6=\
Error: LinkageError occurred while loading main class {0}\n\
\t{1}
java.launcher.cls.error7=\
Error: Unable to initialize main class {0}\n\
Caused by: {1}: {2}
java.launcher.jar.error1=\
Error: An unexpected error occurred while trying to open file {0}
java.launcher.jar.error2=manifest not found in {0}
@ -222,7 +225,10 @@ java.launcher.module.error1=\
java.launcher.module.error2=\
Error: Could not find or load main class {0} in module {1}
java.launcher.module.error3=\
Error: Unable to load main class {0} from module {1}\n\
Error: Unable to load main class {0} in module {1}\n\
\t{2}
java.launcher.module.error4=\
{0} not found
java.launcher.module.error5=\
Error: Unable to initialize main class {0} in module {1}\n\
Caused by: {1}: {2}

View File

@ -23,7 +23,7 @@
/**
* @test
* @bug 8174694
* @bug 8174694 8181033
* @summary improve error message shown when main class can't be loaded
* @compile MainClassCantBeLoadedTest.java
* @run main MainClassCantBeLoadedTest
@ -85,6 +85,68 @@ public class MainClassCantBeLoadedTest extends TestHelper {
System.err.println(trExecution);
}
@Test
void testFailToInitializeMainClass() throws Exception {
if (!isEnglishLocale()) {
return;
}
File cwd = new File(".");
File srcDir = new File(cwd, "src");
if (srcDir.exists()) {
recursiveDelete(srcDir);
}
srcDir.mkdirs();
/* we want to generate class C that will resolve additional class
*/
ArrayList<String> scratchpad = new ArrayList<>();
scratchpad.add("public class C {");
scratchpad.add(" public static void main(String... args) {");
scratchpad.add(" try {");
scratchpad.add(" System.out.println(\"loading of restricted class\");");
scratchpad.add(" } catch (Exception e) {");
scratchpad.add(" java.security.Provider p = new com.sun.crypto.provider.SunJCE();");
scratchpad.add(" p.toString();");
scratchpad.add(" }");
scratchpad.add(" }");
scratchpad.add("}");
createFile(new File(srcDir, "C.java"), scratchpad);
// Compile and execute C should succeed
TestResult trCompilation = doExec(javacCmd,
"--add-exports", "java.base/com.sun.crypto.provider=ALL-UNNAMED",
"-d", "out",
new File(srcDir, "C.java").toString());
if (!trCompilation.isOK()) {
System.err.println(trCompilation);
throw new RuntimeException("Error: compiling");
}
TestResult trExecution = doExec(javaCmd,
"--add-exports", "java.base/com.sun.crypto.provider=ALL-UNNAMED",
"-cp", "out", "C");
if (!trExecution.isOK()) {
System.err.println(trExecution);
throw new RuntimeException("Error: executing");
}
// Execute C with security manager will fail with AccessControlException
trExecution = doExec(javaCmd,
"-Djava.security.manager",
"--add-exports", "java.base/com.sun.crypto.provider=ALL-UNNAMED",
"-cp", "out", "C");
// then this error message should be generated
trExecution.contains("Error: Unable to initialize main class C");
trExecution.contains("Caused by: java.security.AccessControlException: " +
"access denied (\"java.lang.RuntimePermission\"" +
" \"accessClassInPackage.com.sun.crypto.provider\")");
if (!trExecution.testStatus)
System.err.println(trExecution);
}
public static void main(String[] args) throws Exception {
MainClassCantBeLoadedTest a = new MainClassCantBeLoadedTest();
a.run(args);