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

View File

@ -396,7 +396,11 @@ public enum LauncherHelper {
if (mainAttrs == null) {
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 {
if (jarFile != null) {
jarFile.close();

View File

@ -24,7 +24,7 @@
/**
* @test
* @bug 5030233 6214916 6356475 6571029 6684582 6742159 4459600 6758881 6753938
* 6894719 6968053
* 6894719 6968053 7067922
* @summary Argument parsing validation.
* @compile -XDignore.symbol.file Arrrghs.java TestHelper.java
* @run main Arrrghs
@ -362,6 +362,16 @@ public class Arrrghs {
tr.checkPositive();
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
* @throws java.io.FileNotFoundException
@ -374,6 +384,7 @@ public class Arrrghs {
runBasicErrorMessageTests();
runMainMethodTests();
test6894719();
test7067922();
runDiagOptionTests();
if (TestHelper.testExitValue > 0) {
System.out.println("Total of " + TestHelper.testExitValue + " failed");

View File

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