提交 d2b279b0 编写于 作者: K ksrini

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
上级 2723abd3
...@@ -151,7 +151,7 @@ public enum LauncherHelper { ...@@ -151,7 +151,7 @@ public enum LauncherHelper {
throw new IOException("no main mainifest attributes, in " + throw new IOException("no main mainifest attributes, in " +
jarname); jarname);
} }
return mainAttrs.getValue(MAIN_CLASS); return mainAttrs.getValue(MAIN_CLASS).trim();
} finally { } finally {
if (jarFile != null) { if (jarFile != null) {
jarFile.close(); jarFile.close();
...@@ -207,10 +207,9 @@ public enum LauncherHelper { ...@@ -207,10 +207,9 @@ public enum LauncherHelper {
throw new RuntimeException("Main method not found in " + classname); throw new RuntimeException("Main method not found in " + classname);
} }
/* /*
* Usually the getMethod (above) will choose the correct method, based * getMethod (above) will choose the correct method, based
* on its modifiers and parameter types, the only check required is the * on its name and parameter type, however, we still have to
* getReturnType check as getMethod does not check for this, all the * ensure that the method is static and returns a void.
* other modifier tests are redundant, and are simply here for safety.
*/ */
int mod = method.getModifiers(); int mod = method.getModifiers();
if (!Modifier.isStatic(mod)) { if (!Modifier.isStatic(mod)) {
...@@ -219,12 +218,6 @@ public enum LauncherHelper { ...@@ -219,12 +218,6 @@ public enum LauncherHelper {
throw new RuntimeException("Main method is not static in class " + throw new RuntimeException("Main method is not static in class " +
classname); 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(); Class<?> rType = method.getReturnType();
if (!rType.isPrimitive() || !rType.getName().equals("void")) { if (!rType.isPrimitive() || !rType.getName().equals("void")) {
ostream.println(getLocalizedMessage("java.launcher.cls.error3", ostream.println(getLocalizedMessage("java.launcher.cls.error3",
......
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
/** /**
* @test * @test
* @compile -XDignore.symbol.file Arrrghs.java TestHelper.java * @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 * @run main Arrrghs
* @summary Argument parsing validation. * @summary Argument parsing validation.
*/ */
...@@ -232,7 +232,8 @@ public class Arrrghs { ...@@ -232,7 +232,8 @@ public class Arrrghs {
TestHelper.TestResult tr = null; TestHelper.TestResult tr = null;
// a missing class // 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 = TestHelper.doExec(TestHelper.javaCmd, "-jar", "some.jar");
tr.contains("MIA"); tr.contains("MIA");
System.out.println(tr); System.out.println(tr);
...@@ -276,13 +277,13 @@ public class Arrrghs { ...@@ -276,13 +277,13 @@ public class Arrrghs {
// incorrect method type - non-static // incorrect method type - non-static
TestHelper.createJar(new File("some.jar"), new File("Foo"), 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 = 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); System.out.println(tr);
// use classpath to check // use classpath to check
tr = TestHelper.doExec(TestHelper.javaCmd, "-cp", "some.jar", "Foo"); 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); System.out.println(tr);
// amongst a potpourri of kindred main methods, is the right one chosen ? // amongst a potpourri of kindred main methods, is the right one chosen ?
...@@ -300,6 +301,13 @@ public class Arrrghs { ...@@ -300,6 +301,13 @@ public class Arrrghs {
tr = TestHelper.doExec(TestHelper.javaCmd, "-cp", "some.jar", "Foo"); tr = TestHelper.doExec(TestHelper.javaCmd, "-cp", "some.jar", "Foo");
tr.contains("THE_CHOSEN_ONE"); tr.contains("THE_CHOSEN_ONE");
System.out.println(tr); 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);
} }
/** /**
......
...@@ -72,8 +72,8 @@ public enum TestHelper { ...@@ -72,8 +72,8 @@ public enum TestHelper {
} }
/* /*
* A generic jar file creator which creates the java file, compiles it * A convenience method to create a java file, compile and jar it up, using
* and jar's it up for use. * the sole class file name in the jar, as the Main-Class attribute value.
*/ */
static void createJar(File jarName, File mainClass, String... mainDefs) static void createJar(File jarName, File mainClass, String... mainDefs)
throws FileNotFoundException { throws FileNotFoundException {
...@@ -81,11 +81,13 @@ public enum TestHelper { ...@@ -81,11 +81,13 @@ public enum TestHelper {
} }
/* /*
* A method which takes manifest entry to specify a specific manifest * A generic jar file creator to create a java file, compile it
* Main-Class name. * 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) static void createJar(String mEntry, File jarName, File mainClass,
throws FileNotFoundException { String... mainDefs) throws FileNotFoundException {
if (jarName.exists()) { if (jarName.exists()) {
jarName.delete(); jarName.delete();
} }
...@@ -105,10 +107,7 @@ public enum TestHelper { ...@@ -105,10 +107,7 @@ public enum TestHelper {
if (compiler.run(null, null, null, compileArgs) != 0) { if (compiler.run(null, null, null, compileArgs) != 0) {
throw new RuntimeException("compilation failed " + mainClass + ".java"); throw new RuntimeException("compilation failed " + mainClass + ".java");
} }
if (mEntry == null) {
if (mEntry == null && mainDefs == null) {
mEntry = "MIA";
} else {
mEntry = mainClass.getName(); mEntry = mainClass.getName();
} }
String jarArgs[] = { String jarArgs[] = {
...@@ -125,7 +124,7 @@ public enum TestHelper { ...@@ -125,7 +124,7 @@ public enum TestHelper {
} }
/* /*
* 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) { static TestResult doExec(String...cmds) {
String cmdStr = ""; String cmdStr = "";
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册