changeset 4343:440d5a3cfdf8

7067922: (launcher) java -jar throws NPE if JAR file does not contain Main-Class attribute Reviewed-by: darcy, ohair, alanb, mduigou
author ksrini
date Tue, 19 Jul 2011 10:58:50 -0700
parents 175f98d43a12
children a5ea1f537169
files src/share/classes/sun/launcher/LauncherHelper.java test/tools/launcher/Arrrghs.java test/tools/launcher/TestHelper.java
diffstat 3 files changed, 35 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/sun/launcher/LauncherHelper.java	Fri Jul 15 16:38:41 2011 -0700
+++ b/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/test/tools/launcher/Arrrghs.java	Fri Jul 15 16:38:41 2011 -0700
+++ b/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/test/tools/launcher/TestHelper.java	Fri Jul 15 16:38:41 2011 -0700
+++ b/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();