7067922: (launcher) java -jar throws NPE if JAR file does not contain Main-Class attribute

Reviewed-by: darcy, ohair, alanb, mduigou
This commit is contained in:
Kumar Srinivasan 2011-07-19 10:58:50 -07:00
parent 07381cf2d4
commit 2b42b78393
3 changed files with 35 additions and 12 deletions
jdk
src/share/classes/sun/launcher
test/tools/launcher

@ -396,7 +396,11 @@ public enum LauncherHelper {
if (mainAttrs == null) { if (mainAttrs == null) {
abort(ostream, null, "java.launcher.jar.error3", jarname); abort(ostream, null, "java.launcher.jar.error3", jarname);
} }
return mainAttrs.getValue(MAIN_CLASS).trim(); String mainValue = mainAttrs.getValue(MAIN_CLASS);
if (mainValue == null) {
abort(ostream, null, "java.launcher.jar.error3", jarname);
}
return mainValue.trim();
} finally { } finally {
if (jarFile != null) { if (jarFile != null) {
jarFile.close(); jarFile.close();

@ -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 6968053 * 6894719 6968053 7067922
* @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
@ -362,6 +362,16 @@ public class Arrrghs {
tr.checkPositive(); tr.checkPositive();
System.out.println(tr); System.out.println(tr);
} }
static void test7067922() {
// a missing manifest entry 7067922
TestHelper.TestResult tr = null;
TestHelper.createJar("cvf", "missingmainentry.jar", ".");
tr = TestHelper.doExec(TestHelper.javaCmd, "-jar", "missingmainentry.jar");
tr.contains("no main manifest attribute");
System.out.println(tr);
}
/** /**
* @param args the command line arguments * @param args the command line arguments
* @throws java.io.FileNotFoundException * @throws java.io.FileNotFoundException
@ -374,6 +384,7 @@ public class Arrrghs {
runBasicErrorMessageTests(); runBasicErrorMessageTests();
runMainMethodTests(); runMainMethodTests();
test6894719(); test6894719();
test7067922();
runDiagOptionTests(); 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");

@ -171,15 +171,15 @@ public enum TestHelper {
if (jarName.exists()) { if (jarName.exists()) {
jarName.delete(); jarName.delete();
} }
PrintStream ps = new PrintStream(new FileOutputStream(mainClass + ".java")); try (PrintStream ps = new PrintStream(new FileOutputStream(mainClass + ".java"))) {
ps.println("public class Foo {"); ps.println("public class Foo {");
if (mainDefs != null) { if (mainDefs != null) {
for (String x : mainDefs) { for (String x : mainDefs) {
ps.println(x); ps.println(x);
}
} }
ps.println("}");
} }
ps.println("}");
ps.close();
String compileArgs[] = { String compileArgs[] = {
mainClass + ".java" mainClass + ".java"
@ -196,12 +196,20 @@ public enum TestHelper {
mEntry, mEntry,
mainClass.getName() + ".class" mainClass.getName() + ".class"
}; };
createJar(jarArgs);
}
static void createJar(String... args) {
sun.tools.jar.Main jarTool = sun.tools.jar.Main jarTool =
new sun.tools.jar.Main(System.out, System.err, "JarCreator"); new sun.tools.jar.Main(System.out, System.err, "JarCreator");
if (!jarTool.run(jarArgs)) { if (!jarTool.run(args)) {
throw new RuntimeException("jar creation failed " + jarName); String message = "jar creation failed with command:";
for (String x : args) {
message = message.concat(" " + x);
}
throw new RuntimeException(message);
} }
} }
static void copyFile(File src, File dst) throws IOException { static void copyFile(File src, File dst) throws IOException {
Path parent = dst.toPath().getParent(); Path parent = dst.toPath().getParent();