8232281: jpackage is not always reporting an error when no main class specified JDK-8200758-branch
authorherrick
Fri, 18 Oct 2019 11:00:57 -0400
branchJDK-8200758-branch
changeset 58695 64adf683bc7b
parent 58671 3b578a5976df
child 58696 61c44899b4eb
8232281: jpackage is not always reporting an error when no main class specified Reviewed-by: asemenyuk, asemenuk
src/jdk.jpackage/share/classes/jdk/jpackage/internal/AbstractAppImageBuilder.java
src/jdk.jpackage/share/classes/jdk/jpackage/internal/JLinkBundlerHelper.java
src/jdk.jpackage/share/classes/jdk/jpackage/internal/StandardBundlerParam.java
src/jdk.jpackage/windows/classes/jdk/jpackage/internal/WindowsAppImageBuilder.java
test/jdk/tools/jpackage/share/jdk/jpackage/tests/MainClassTest.java
--- a/src/jdk.jpackage/share/classes/jdk/jpackage/internal/AbstractAppImageBuilder.java	Thu Oct 17 08:00:37 2019 -0400
+++ b/src/jdk.jpackage/share/classes/jdk/jpackage/internal/AbstractAppImageBuilder.java	Fri Oct 18 11:00:57 2019 -0400
@@ -116,7 +116,8 @@
                     mainJarType == ModFile.ModType.ModularJar)) {
                 out.println("app.mainmodule=" + mainModule);
             } else {
-                String mainClass = JLinkBundlerHelper.getMainClass(params);
+                String mainClass =
+                        StandardBundlerParam.MAIN_CLASS.fetchFrom(params);
                 // If the app is contained in an unnamed jar then launch it the
                 // legacy way and the main class string must be
                 // of the format com/foo/Main
--- a/src/jdk.jpackage/share/classes/jdk/jpackage/internal/JLinkBundlerHelper.java	Thu Oct 17 08:00:37 2019 -0400
+++ b/src/jdk.jpackage/share/classes/jdk/jpackage/internal/JLinkBundlerHelper.java	Fri Oct 18 11:00:57 2019 -0400
@@ -90,13 +90,13 @@
         return result;
     }
 
-    static String getMainClass(Map<String, ? super Object> params) {
-        String result = "";
+    static String getMainClassFromModule(Map<String, ? super Object> params) {
         String mainModule = StandardBundlerParam.MODULE.fetchFrom(params);
         if (mainModule != null)  {
+
             int index = mainModule.indexOf("/");
             if (index > 0) {
-                result = mainModule.substring(index + 1);
+                return mainModule.substring(index + 1);
             } else {
                 ModuleDescriptor descriptor =
                         JLinkBundlerHelper.getMainModuleDescription(params);
@@ -107,21 +107,12 @@
                                     "message.module-class"),
                                     mainClass.get(),
                                     JLinkBundlerHelper.getMainModule(params)));
-                        result = mainClass.get();
+                        return mainClass.get();
                     }
                 }
             }
-        } else {
-            RelativeFileSet fileset =
-                    StandardBundlerParam.MAIN_JAR.fetchFrom(params);
-            if (fileset != null) {
-                result = StandardBundlerParam.MAIN_CLASS.fetchFrom(params);
-            } else {
-                // possibly app-image
-            }
         }
-
-        return result;
+        return null;
     }
 
     static String getMainModule(Map<String, ? super Object> params) {
@@ -152,11 +143,6 @@
             AbstractAppImageBuilder imageBuilder)
             throws IOException, Exception {
 
-        // we might be able to build it (with no main class) but it won't run
-        if (StandardBundlerParam.MAIN_CLASS.fetchFrom(params) == null) {
-            throw new PackagerException("ERR_NoMainClass");
-        }
-
         List<Path> modulePath =
                 StandardBundlerParam.MODULE_PATH.fetchFrom(params);
         Set<String> addModules =
--- a/src/jdk.jpackage/share/classes/jdk/jpackage/internal/StandardBundlerParam.java	Thu Oct 17 08:00:37 2019 -0400
+++ b/src/jdk.jpackage/share/classes/jdk/jpackage/internal/StandardBundlerParam.java	Fri Oct 18 11:00:57 2019 -0400
@@ -156,10 +156,8 @@
                         String s = (String) params.get(
                                 BundleParams.PARAM_APPLICATION_CLASS);
                         if (s == null) {
-                            s = JLinkBundlerHelper.getMainClass(params);
-                            if (s.length() == 0) {
-                                s = null;
-                            }
+                            s = JLinkBundlerHelper.getMainClassFromModule(
+                                    params);
                         }
                         return s;
                     },
@@ -638,25 +636,31 @@
         boolean hasAppImage = params.containsKey(PREDEFINED_APP_IMAGE.getID());
 
         if (hasMainClass && hasMainJar && hasMainJarClassPath ||
-               hasModule || hasAppImage || isRuntimeInstaller(params)) {
+               hasAppImage || isRuntimeInstaller(params)) {
             return;
         }
-
-        extractMainClassInfoFromAppResources(params);
-
-        if (!params.containsKey(MAIN_CLASS.getID())) {
-            if (hasMainJar) {
+        if (hasModule) {
+            if (JLinkBundlerHelper.getMainClassFromModule(params) == null) {
                 throw new ConfigException(
-                        MessageFormat.format(I18N.getString(
-                        "error.no-main-class-with-main-jar"),
-                        MAIN_JAR.fetchFrom(params)),
-                        MessageFormat.format(I18N.getString(
-                        "error.no-main-class-with-main-jar.advice"),
-                        MAIN_JAR.fetchFrom(params)));
-            } else {
-                throw new ConfigException(
-                        I18N.getString("error.no-main-class"),
-                        I18N.getString("error.no-main-class.advice"));
+                        I18N.getString("ERR_NoMainClass"), null);
+            }
+        } else {
+            extractMainClassInfoFromAppResources(params);
+
+            if (!params.containsKey(MAIN_CLASS.getID())) {
+                if (hasMainJar) {
+                    throw new ConfigException(
+                            MessageFormat.format(I18N.getString(
+                            "error.no-main-class-with-main-jar"),
+                            MAIN_JAR.fetchFrom(params)),
+                            MessageFormat.format(I18N.getString(
+                            "error.no-main-class-with-main-jar.advice"),
+                            MAIN_JAR.fetchFrom(params)));
+                } else {
+                    throw new ConfigException(
+                            I18N.getString("error.no-main-class"),
+                            I18N.getString("error.no-main-class.advice"));
+                }
             }
         }
     }
--- a/src/jdk.jpackage/windows/classes/jdk/jpackage/internal/WindowsAppImageBuilder.java	Thu Oct 17 08:00:37 2019 -0400
+++ b/src/jdk.jpackage/windows/classes/jdk/jpackage/internal/WindowsAppImageBuilder.java	Fri Oct 18 11:00:57 2019 -0400
@@ -308,7 +308,7 @@
                 }
                 if (WindowsDefender.isThereAPotentialWindowsDefenderIssue(
                         tempDirectory)) {
-                    Log.error(MessageFormat.format(I18N.getString(
+                    Log.verbose(MessageFormat.format(I18N.getString(
                             "message.potential.windows.defender.issue"),
                             tempDirectory));
                 }
--- a/test/jdk/tools/jpackage/share/jdk/jpackage/tests/MainClassTest.java	Thu Oct 17 08:00:37 2019 -0400
+++ b/test/jdk/tools/jpackage/share/jdk/jpackage/tests/MainClassTest.java	Fri Oct 18 11:00:57 2019 -0400
@@ -54,7 +54,6 @@
  *  --jpt-run=jdk.jpackage.tests.MainClassTest
  *  --jpt-space-subst=_
  *  --jpt-exclude=modular=y;_main-class=n;_jar-main-class=b;_jlink=y
- *  --jpt-exclude=modular=y;_main-class=n;_jar-main-class=n;_jlink=n
  */
 
 public final class MainClassTest {