changeset 653:906b5be0eca5

4459600: java -jar fails to run Main-Class if classname followed by whitespace. Summary: Fixed whitespace trimming in the manifest as well as post review comments on CR: 6742159 Reviewed-by: darcy, dholmes
author ksrini
date Wed, 01 Oct 2008 09:04:42 -0700
parents d74db4a9799a
children 8e14a7bd6753
files src/share/classes/sun/launcher/LauncherHelper.java test/tools/launcher/Arrrghs.java test/tools/launcher/TestHelper.java
diffstat 3 files changed, 27 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/sun/launcher/LauncherHelper.java	Fri Oct 03 09:36:05 2008 -0700
+++ b/src/share/classes/sun/launcher/LauncherHelper.java	Wed Oct 01 09:04:42 2008 -0700
@@ -151,7 +151,7 @@
                 throw new IOException("no main mainifest attributes, in " +
                         jarname);
             }
-            return mainAttrs.getValue(MAIN_CLASS);
+            return mainAttrs.getValue(MAIN_CLASS).trim();
         } finally {
             if (jarFile != null) {
                 jarFile.close();
@@ -207,10 +207,9 @@
             throw new RuntimeException("Main method not found in " + classname);
         }
         /*
-         * Usually the getMethod (above) will choose the correct method, based
-         * on its modifiers and parameter types, the only check required is the
-         * getReturnType check as getMethod does not check for this, all the
-         * other modifier tests are redundant, and are simply here for safety.
+         * getMethod (above) will choose the correct method, based
+         * on its name and parameter type, however, we still have to
+         * ensure that the method is static and returns a void.
          */
         int mod = method.getModifiers();
         if (!Modifier.isStatic(mod)) {
@@ -219,12 +218,6 @@
             throw new RuntimeException("Main method is not static in class " +
                     classname);
         }
-        if (!Modifier.isPublic(mod)) {
-            ostream.println(getLocalizedMessage("java.launcher.cls.error2",
-                    "public", classname));
-            throw new RuntimeException("Main method is not public in class " +
-                    classname);
-        }
         Class<?> rType = method.getReturnType();
         if (!rType.isPrimitive() || !rType.getName().equals("void")) {
             ostream.println(getLocalizedMessage("java.launcher.cls.error3",
--- a/test/tools/launcher/Arrrghs.java	Fri Oct 03 09:36:05 2008 -0700
+++ b/test/tools/launcher/Arrrghs.java	Wed Oct 01 09:04:42 2008 -0700
@@ -24,7 +24,7 @@
 /**
  * @test
  * @compile  -XDignore.symbol.file Arrrghs.java TestHelper.java
- * @bug 5030233 6214916 6356475 6571029 6684582
+ * @bug 5030233 6214916 6356475 6571029 6684582 6742159 4459600
  * @run main Arrrghs
  * @summary Argument parsing validation.
  */
@@ -232,7 +232,8 @@
         TestHelper.TestResult tr = null;
 
         // a missing class
-        TestHelper.createJar(new File("some.jar"), new File("Foo"), (String[])null);
+        TestHelper.createJar("MIA", new File("some.jar"), new File("Foo"),
+                (String[])null);
         tr = TestHelper.doExec(TestHelper.javaCmd, "-jar", "some.jar");
         tr.contains("MIA");
         System.out.println(tr);
@@ -276,13 +277,13 @@
 
         // incorrect method type - non-static
          TestHelper.createJar(new File("some.jar"), new File("Foo"),
-                "public void main(Object[] args){}");
+                "public void main(String[] args){}");
         tr = TestHelper.doExec(TestHelper.javaCmd, "-jar", "some.jar");
-        tr.contains("Error: Main method not found in class Foo");
+        tr.contains("Error: Main method is not static in class Foo");
         System.out.println(tr);
         // use classpath to check
         tr = TestHelper.doExec(TestHelper.javaCmd, "-cp", "some.jar", "Foo");
-        tr.contains("Error: Main method not found in class Foo");
+        tr.contains("Error: Main method is not static in class Foo");
         System.out.println(tr);
 
         // amongst a potpourri of kindred main methods, is the right one chosen ?
@@ -300,6 +301,13 @@
         tr = TestHelper.doExec(TestHelper.javaCmd, "-cp", "some.jar", "Foo");
         tr.contains("THE_CHOSEN_ONE");
         System.out.println(tr);
+
+        // test for extraneous whitespace in the Main-Class attribute
+        TestHelper.createJar(" Foo ", new File("some.jar"), new File("Foo"),
+                "public static void main(String... args){}");
+        tr = TestHelper.doExec(TestHelper.javaCmd, "-jar", "some.jar");
+        tr.checkPositive();
+        System.out.println(tr);
     }
 
     /**
--- a/test/tools/launcher/TestHelper.java	Fri Oct 03 09:36:05 2008 -0700
+++ b/test/tools/launcher/TestHelper.java	Wed Oct 01 09:04:42 2008 -0700
@@ -72,8 +72,8 @@
     }
 
     /*
-     * A generic jar file creator which creates the java file, compiles it
-     * and jar's it up for use.
+     * A convenience method to create a java file, compile and jar it up, using
+     * the sole class file name in the jar, as the Main-Class attribute value.
      */
     static void createJar(File jarName, File mainClass, String... mainDefs)
             throws FileNotFoundException {
@@ -81,11 +81,13 @@
     }
 
     /*
-     * A method which takes manifest entry to specify a specific manifest
-     * Main-Class name.
+     * A generic jar file creator to create a java file, compile it
+     * and jar it up, a specific Main-Class entry name in the
+     * manifest can be specified or a null to use the sole class file name
+     * as the Main-Class attribute value.
      */
-    static void createJar(String mEntry, File jarName, File mainClass, String... mainDefs)
-            throws FileNotFoundException {
+    static void createJar(String mEntry, File jarName, File mainClass,
+            String... mainDefs) throws FileNotFoundException {
         if (jarName.exists()) {
             jarName.delete();
         }
@@ -105,10 +107,7 @@
         if (compiler.run(null, null, null, compileArgs) != 0) {
             throw new RuntimeException("compilation failed " + mainClass + ".java");
         }
-
-        if (mEntry == null && mainDefs == null) {
-            mEntry = "MIA";
-        } else {
+        if (mEntry == null) {
             mEntry = mainClass.getName();
         }
         String jarArgs[] = {
@@ -125,7 +124,7 @@
     }
 
     /*
-     * A method which executes a java cmd and returs the results in a container
+     * A method which executes a java cmd and returns the results in a container
      */
     static TestResult doExec(String...cmds) {
         String cmdStr = "";