提交 6bcdc301 编写于 作者: K ksrini

6968053: (launcher) hide exceptions under certain launcher failures

Reviewed-by: mchung
上级 729dd870
...@@ -244,6 +244,7 @@ JLI_Launch(int argc, char ** argv, /* main argc, argc */ ...@@ -244,6 +244,7 @@ JLI_Launch(int argc, char ** argv, /* main argc, argc */
for (i = 0; i < argc ; i++) { for (i = 0; i < argc ; i++) {
printf("argv[%d] = %s\n", i, argv[i]); printf("argv[%d] = %s\n", i, argv[i]);
} }
AddOption("-Dsun.java.launcher.diag=true", NULL);
} }
CreateExecutionEnvironment(&argc, &argv, CreateExecutionEnvironment(&argc, &argv,
...@@ -1009,6 +1010,8 @@ ParseArguments(int *pargc, char ***pargv, ...@@ -1009,6 +1010,8 @@ ParseArguments(int *pargc, char ***pargv,
} else if (JLI_StrCmp(arg, "-XshowSettings") == 0 || } else if (JLI_StrCmp(arg, "-XshowSettings") == 0 ||
JLI_StrCCmp(arg, "-XshowSettings:") == 0) { JLI_StrCCmp(arg, "-XshowSettings:") == 0) {
showSettings = arg; showSettings = arg;
} else if (JLI_StrCmp(arg, "-Xdiag") == 0) {
AddOption("-Dsun.java.launcher.diag=true", NULL);
/* /*
* The following case provide backward compatibility with old-style * The following case provide backward compatibility with old-style
* command line options. * command line options.
......
...@@ -63,8 +63,6 @@ import java.util.jar.Manifest; ...@@ -63,8 +63,6 @@ import java.util.jar.Manifest;
public enum LauncherHelper { public enum LauncherHelper {
INSTANCE; INSTANCE;
private static final String defaultBundleName =
"sun.launcher.resources.launcher";
private static final String MAIN_CLASS = "Main-Class"; private static final String MAIN_CLASS = "Main-Class";
private static StringBuilder outBuf = new StringBuilder(); private static StringBuilder outBuf = new StringBuilder();
...@@ -76,11 +74,14 @@ public enum LauncherHelper { ...@@ -76,11 +74,14 @@ public enum LauncherHelper {
private static final String PROP_SETTINGS = "Property settings:"; private static final String PROP_SETTINGS = "Property settings:";
private static final String LOCALE_SETTINGS = "Locale settings:"; private static final String LOCALE_SETTINGS = "Locale settings:";
private static synchronized ResourceBundle getLauncherResourceBundle() { // sync with java.c and sun.misc.VM
if (javarb == null) { private static final String diagprop = "sun.java.launcher.diag";
javarb = ResourceBundle.getBundle(defaultBundleName);
} private static final String defaultBundleName =
return javarb; "sun.launcher.resources.launcher";
private static class ResourceBundleHolder {
private static final ResourceBundle RB =
ResourceBundle.getBundle(defaultBundleName);
} }
/* /*
...@@ -308,7 +309,7 @@ public enum LauncherHelper { ...@@ -308,7 +309,7 @@ public enum LauncherHelper {
* apply any arguments that we might pass. * apply any arguments that we might pass.
*/ */
private static String getLocalizedMessage(String key, Object... args) { private static String getLocalizedMessage(String key, Object... args) {
String msg = getLauncherResourceBundle().getString(key); String msg = ResourceBundleHolder.RB.getString(key);
return (args != null) ? MessageFormat.format(msg, args) : msg; return (args != null) ? MessageFormat.format(msg, args) : msg;
} }
...@@ -380,25 +381,29 @@ public enum LauncherHelper { ...@@ -380,25 +381,29 @@ public enum LauncherHelper {
File.pathSeparator)); File.pathSeparator));
} }
static String getMainClassFromJar(String jarname) throws IOException { static String getMainClassFromJar(PrintStream ostream, String jarname) {
JarFile jarFile = null;
try { try {
jarFile = new JarFile(jarname); JarFile jarFile = null;
Manifest manifest = jarFile.getManifest(); try {
if (manifest == null) { jarFile = new JarFile(jarname);
throw new IOException("manifest not found in " + jarname); Manifest manifest = jarFile.getManifest();
} if (manifest == null) {
Attributes mainAttrs = manifest.getMainAttributes(); abort(ostream, null, "java.launcher.jar.error2", jarname);
if (mainAttrs == null) { }
throw new IOException("no main mainifest attributes, in " + Attributes mainAttrs = manifest.getMainAttributes();
jarname); if (mainAttrs == null) {
} abort(ostream, null, "java.launcher.jar.error3", jarname);
return mainAttrs.getValue(MAIN_CLASS).trim(); }
} finally { return mainAttrs.getValue(MAIN_CLASS).trim();
if (jarFile != null) { } finally {
jarFile.close(); if (jarFile != null) {
jarFile.close();
}
} }
} catch (IOException ioe) {
abort(ostream, ioe, "java.launcher.jar.error1", jarname);
} }
return null;
} }
...@@ -409,6 +414,20 @@ public enum LauncherHelper { ...@@ -409,6 +414,20 @@ public enum LauncherHelper {
private static final int LM_CLASS = 1; private static final int LM_CLASS = 1;
private static final int LM_JAR = 2; private static final int LM_JAR = 2;
static void abort(PrintStream ostream, Throwable t, String msgKey, Object... args) {
if (msgKey != null) {
ostream.println(getLocalizedMessage(msgKey, args));
}
if (sun.misc.VM.getSavedProperty(diagprop) != null) {
if (t != null) {
t.printStackTrace();
} else {
Thread.currentThread().dumpStack();
}
}
System.exit(1);
}
/** /**
* This method does the following: * This method does the following:
* 1. gets the classname from a Jar's manifest, if necessary * 1. gets the classname from a Jar's manifest, if necessary
...@@ -426,39 +445,31 @@ public enum LauncherHelper { ...@@ -426,39 +445,31 @@ public enum LauncherHelper {
* @param isJar * @param isJar
* @param name * @param name
* @return * @return
* @throws java.io.IOException
*/ */
public static Class<?> checkAndLoadMain(boolean printToStderr, public static Class<?> checkAndLoadMain(boolean printToStderr,
int mode, int mode,
String what) throws IOException String what) {
{ final PrintStream ostream = (printToStderr) ? System.err : System.out;
final ClassLoader ld = ClassLoader.getSystemClassLoader();
ClassLoader ld = ClassLoader.getSystemClassLoader();
// get the class name // get the class name
String cn = null; String cn = null;
switch (mode) { switch (mode) {
case LM_CLASS: case LM_CLASS:
cn = what; cn = what;
break; break;
case LM_JAR: case LM_JAR:
cn = getMainClassFromJar(what); cn = getMainClassFromJar(ostream, what);
break; break;
default: default:
throw new InternalError("" + mode + ": Unknown launch mode"); // should never happen
throw new InternalError("" + mode + ": Unknown launch mode");
} }
cn = cn.replace('/', '.'); cn = cn.replace('/', '.');
PrintStream ostream = (printToStderr) ? System.err : System.out;
Class<?> c = null; Class<?> c = null;
try { try {
c = ld.loadClass(cn); c = ld.loadClass(cn);
} catch (ClassNotFoundException cnfe) { } catch (ClassNotFoundException cnfe) {
ostream.println(getLocalizedMessage("java.launcher.cls.error1", abort(ostream, cnfe, "java.launcher.cls.error1", cn);
cn));
NoClassDefFoundError ncdfe = new NoClassDefFoundError(cn);
ncdfe.initCause(cnfe);
throw ncdfe;
} }
signatureDiagnostic(ostream, c); signatureDiagnostic(ostream, c);
return c; return c;
...@@ -470,9 +481,7 @@ public enum LauncherHelper { ...@@ -470,9 +481,7 @@ public enum LauncherHelper {
try { try {
method = clazz.getMethod("main", String[].class); method = clazz.getMethod("main", String[].class);
} catch (NoSuchMethodException nsme) { } catch (NoSuchMethodException nsme) {
ostream.println(getLocalizedMessage("java.launcher.cls.error4", abort(ostream, null, "java.launcher.cls.error4", classname);
classname));
throw new RuntimeException("Main method not found in " + classname);
} }
/* /*
* getMethod (above) will choose the correct method, based * getMethod (above) will choose the correct method, based
...@@ -481,17 +490,10 @@ public enum LauncherHelper { ...@@ -481,17 +490,10 @@ public enum LauncherHelper {
*/ */
int mod = method.getModifiers(); int mod = method.getModifiers();
if (!Modifier.isStatic(mod)) { if (!Modifier.isStatic(mod)) {
ostream.println(getLocalizedMessage("java.launcher.cls.error2", abort(ostream, null, "java.launcher.cls.error2", "static", classname);
"static", classname));
throw new RuntimeException("Main method is not static in class " +
classname);
} }
if (method.getReturnType() != java.lang.Void.TYPE) { if (method.getReturnType() != java.lang.Void.TYPE) {
ostream.println(getLocalizedMessage("java.launcher.cls.error3", abort(ostream, null, "java.launcher.cls.error3", classname);
classname));
throw new RuntimeException("Main method must return a value" +
" of type void in class " +
classname);
} }
return; return;
} }
......
...@@ -84,6 +84,7 @@ java.launcher.X.usage=\ ...@@ -84,6 +84,7 @@ java.launcher.X.usage=\
\ append to end of bootstrap class path\n\ \ append to end of bootstrap class path\n\
\ -Xbootclasspath/p:<directories and zip/jar files separated by {0}>\n\ \ -Xbootclasspath/p:<directories and zip/jar files separated by {0}>\n\
\ prepend in front of bootstrap class path\n\ \ prepend in front of bootstrap class path\n\
\ -Xdiag show additional diagnostic messages\n\
\ -Xnoclassgc disable class garbage collection\n\ \ -Xnoclassgc disable class garbage collection\n\
\ -Xincgc enable incremental garbage collection\n\ \ -Xincgc enable incremental garbage collection\n\
\ -Xloggc:<file> log GC status to a file with time stamps\n\ \ -Xloggc:<file> log GC status to a file with time stamps\n\
...@@ -109,7 +110,7 @@ java.launcher.X.usage=\ ...@@ -109,7 +110,7 @@ java.launcher.X.usage=\
The -X options are non-standard and subject to change without notice.\n The -X options are non-standard and subject to change without notice.\n
java.launcher.cls.error1=\ java.launcher.cls.error1=\
Error: Could not find main class {0} Error: Could not find or load main class {0}
java.launcher.cls.error2=\ java.launcher.cls.error2=\
Error: Main method is not {0} in class {1}, please define the main method as:\n\ Error: Main method is not {0} in class {1}, please define the main method as:\n\
\ public static void main(String[] args) \ public static void main(String[] args)
...@@ -120,5 +121,7 @@ java.launcher.cls.error3=\ ...@@ -120,5 +121,7 @@ java.launcher.cls.error3=\
java.launcher.cls.error4=\ java.launcher.cls.error4=\
Error: Main method not found in class {0}, please define the main method as:\n\ Error: Main method not found in class {0}, please define the main method as:\n\
\ public static void main(String[] args) \ public static void main(String[] args)
java.launcher.jar.error1=\
Error: An unexpected error occurred while trying to open file {0}
java.launcher.jar.error2=manifest not found in {0}
java.launcher.jar.error3=no main manifest attribute, in {0}
...@@ -235,6 +235,9 @@ public class VM { ...@@ -235,6 +235,9 @@ public class VM {
return savedProps.getProperty(key); return savedProps.getProperty(key);
} }
// TODO: the Property Management needs to be refactored and
// the appropriate prop keys need to be accessible to the
// calling classes to avoid duplication of keys.
private static final Properties savedProps = new Properties(); private static final Properties savedProps = new Properties();
// Save a private copy of the system properties and remove // Save a private copy of the system properties and remove
...@@ -283,6 +286,9 @@ public class VM { ...@@ -283,6 +286,9 @@ public class VM {
// used by java.util.zip.ZipFile // used by java.util.zip.ZipFile
props.remove("sun.zip.disableMemoryMapping"); props.remove("sun.zip.disableMemoryMapping");
// used by sun.launcher.LauncherHelper
props.remove("sun.java.launcher.diag");
} }
// Initialize any miscellenous operating system settings that need to be // Initialize any miscellenous operating system settings that need to be
......
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
/** /**
* @test * @test
* @bug 5030233 6214916 6356475 6571029 6684582 6742159 4459600 6758881 6753938 * @bug 5030233 6214916 6356475 6571029 6684582 6742159 4459600 6758881 6753938
* 6894719 * 6894719 6968053
* @summary Argument parsing validation. * @summary Argument parsing validation.
* @compile -XDignore.symbol.file Arrrghs.java TestHelper.java * @compile -XDignore.symbol.file Arrrghs.java TestHelper.java
* @run main Arrrghs * @run main Arrrghs
...@@ -250,13 +250,11 @@ public class Arrrghs { ...@@ -250,13 +250,11 @@ public class Arrrghs {
TestHelper.createJar("MIA", new File("some.jar"), new File("Foo"), TestHelper.createJar("MIA", new File("some.jar"), new File("Foo"),
(String[])null); (String[])null);
tr = TestHelper.doExec(TestHelper.javaCmd, "-jar", "some.jar"); tr = TestHelper.doExec(TestHelper.javaCmd, "-jar", "some.jar");
tr.contains("Error: Could not find main class MIA"); tr.contains("Error: Could not find or load main class MIA");
tr.contains("java.lang.NoClassDefFoundError: MIA");
System.out.println(tr); System.out.println(tr);
// use classpath to check // use classpath to check
tr = TestHelper.doExec(TestHelper.javaCmd, "-cp", "some.jar", "MIA"); tr = TestHelper.doExec(TestHelper.javaCmd, "-cp", "some.jar", "MIA");
tr.contains("Error: Could not find main class MIA"); tr.contains("Error: Could not find or load main class MIA");
tr.contains("java.lang.NoClassDefFoundError: MIA");
System.out.println(tr); System.out.println(tr);
// incorrect method access // incorrect method access
...@@ -305,12 +303,12 @@ public class Arrrghs { ...@@ -305,12 +303,12 @@ public class Arrrghs {
// amongst a potpourri of kindred main methods, is the right one chosen ? // amongst a potpourri of kindred main methods, is the right one chosen ?
TestHelper.createJar(new File("some.jar"), new File("Foo"), TestHelper.createJar(new File("some.jar"), new File("Foo"),
"void main(Object[] args){}", "void main(Object[] args){}",
"int main(Float[] args){return 1;}", "int main(Float[] args){return 1;}",
"private void main() {}", "private void main() {}",
"private static void main(int x) {}", "private static void main(int x) {}",
"public int main(int argc, String[] argv) {return 1;}", "public int main(int argc, String[] argv) {return 1;}",
"public static void main(String[] args) {System.out.println(\"THE_CHOSEN_ONE\");}"); "public static void main(String[] args) {System.out.println(\"THE_CHOSEN_ONE\");}");
tr = TestHelper.doExec(TestHelper.javaCmd, "-jar", "some.jar"); tr = TestHelper.doExec(TestHelper.javaCmd, "-jar", "some.jar");
tr.contains("THE_CHOSEN_ONE"); tr.contains("THE_CHOSEN_ONE");
System.out.println(tr); System.out.println(tr);
...@@ -326,6 +324,30 @@ public class Arrrghs { ...@@ -326,6 +324,30 @@ public class Arrrghs {
tr.checkPositive(); tr.checkPositive();
System.out.println(tr); System.out.println(tr);
} }
// tests 6968053, ie. we turn on the -Xdiag (for now) flag and check if
// the suppressed stack traces are exposed.
static void runDiagOptionTests() throws FileNotFoundException {
TestHelper.TestResult tr = null;
// a missing class
TestHelper.createJar("MIA", new File("some.jar"), new File("Foo"),
(String[])null);
tr = TestHelper.doExec(TestHelper.javaCmd, "-Xdiag", "-jar", "some.jar");
tr.contains("Error: Could not find or load main class MIA");
tr.contains("java.lang.ClassNotFoundException: MIA");
System.out.println(tr);
// use classpath to check
tr = TestHelper.doExec(TestHelper.javaCmd, "-Xdiag", "-cp", "some.jar", "MIA");
tr.contains("Error: Could not find or load main class MIA");
tr.contains("java.lang.ClassNotFoundException: MIA");
System.out.println(tr);
// a missing class on the classpath
tr = TestHelper.doExec(TestHelper.javaCmd, "-Xdiag", "NonExistentClass");
tr.contains("Error: Could not find or load main class NonExistentClass");
tr.contains("java.lang.ClassNotFoundException: NonExistentClass");
System.out.println(tr);
}
static void test6894719() { static void test6894719() {
// test both arguments to ensure they exist // test both arguments to ensure they exist
...@@ -352,6 +374,7 @@ public class Arrrghs { ...@@ -352,6 +374,7 @@ public class Arrrghs {
runBasicErrorMessageTests(); runBasicErrorMessageTests();
runMainMethodTests(); runMainMethodTests();
test6894719(); test6894719();
runDiagOptionTests();
if (TestHelper.testExitValue > 0) { if (TestHelper.testExitValue > 0) {
System.out.println("Total of " + TestHelper.testExitValue + " failed"); System.out.println("Total of " + TestHelper.testExitValue + " failed");
System.exit(1); System.exit(1);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册