7067922: (launcher) java -jar throws NPE if JAR file does not contain Main-Class attribute
authorksrini
Tue, 19 Jul 2011 10:58:50 -0700
changeset 10126 a375b8714147
parent 10125 c70d99150c40
child 10127 c85d1ec57ee7
7067922: (launcher) java -jar throws NPE if JAR file does not contain Main-Class attribute Reviewed-by: darcy, ohair, alanb, mduigou
jdk/src/share/classes/sun/launcher/LauncherHelper.java
jdk/test/tools/launcher/Arrrghs.java
jdk/test/tools/launcher/TestHelper.java
--- a/jdk/src/share/classes/sun/launcher/LauncherHelper.java	Tue Jul 19 08:21:05 2011 -0700
+++ b/jdk/src/share/classes/sun/launcher/LauncherHelper.java	Tue Jul 19 10:58:50 2011 -0700
@@ -396,7 +396,11 @@
                 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();
--- a/jdk/test/tools/launcher/Arrrghs.java	Tue Jul 19 08:21:05 2011 -0700
+++ b/jdk/test/tools/launcher/Arrrghs.java	Tue Jul 19 10:58:50 2011 -0700
@@ -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 @@
         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 @@
         runBasicErrorMessageTests();
         runMainMethodTests();
         test6894719();
+        test7067922();
         runDiagOptionTests();
         if (TestHelper.testExitValue > 0) {
             System.out.println("Total of " + TestHelper.testExitValue + " failed");
--- a/jdk/test/tools/launcher/TestHelper.java	Tue Jul 19 08:21:05 2011 -0700
+++ b/jdk/test/tools/launcher/TestHelper.java	Tue Jul 19 10:58:50 2011 -0700
@@ -171,15 +171,15 @@
         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 @@
             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();