8231382: Use main class from main module if available
Submitted-by: almatvee
Reviewed-by: herrick, asemenyuk
--- a/src/jdk.jpackage/share/classes/jdk/jpackage/internal/JLinkBundlerHelper.java Thu Sep 26 09:19:46 2019 -0400
+++ b/src/jdk.jpackage/share/classes/jdk/jpackage/internal/JLinkBundlerHelper.java Thu Sep 26 10:37:37 2019 -0400
@@ -97,6 +97,19 @@
int index = mainModule.indexOf("/");
if (index > 0) {
result = mainModule.substring(index + 1);
+ } else {
+ ModuleDescriptor descriptor =
+ JLinkBundlerHelper.getMainModuleDescription(params);
+ if (descriptor != null) {
+ Optional<String> mainClass = descriptor.mainClass();
+ if (mainClass.isPresent()) {
+ Log.verbose(MessageFormat.format(I18N.getString(
+ "message.module-class"),
+ mainClass.get(),
+ JLinkBundlerHelper.getMainModule(params)));
+ result = mainClass.get();
+ }
+ }
}
} else {
RelativeFileSet fileset =
@@ -205,6 +218,23 @@
return null;
}
+ static ModuleDescriptor getMainModuleDescription(Map<String, ? super Object> params) {
+ boolean hasModule = params.containsKey(StandardBundlerParam.MODULE.getID());
+ if (hasModule) {
+ List<Path> modulePath = StandardBundlerParam.MODULE_PATH.fetchFrom(params);
+ if (!modulePath.isEmpty()) {
+ ModuleFinder finder = ModuleFinder.of(modulePath.toArray(new Path[0]));
+ String mainModule = JLinkBundlerHelper.getMainModule(params);
+ Optional<ModuleReference> omref = finder.find(mainModule);
+ if (omref.isPresent()) {
+ return omref.get().descriptor();
+ }
+ }
+ }
+
+ return null;
+ }
+
/*
* Returns the set of modules that would be visible by default for
* a non-modular-aware application consisting of the given elements.
--- a/src/jdk.jpackage/share/classes/jdk/jpackage/internal/StandardBundlerParam.java Thu Sep 26 09:19:46 2019 -0400
+++ b/src/jdk.jpackage/share/classes/jdk/jpackage/internal/StandardBundlerParam.java Thu Sep 26 10:37:37 2019 -0400
@@ -29,8 +29,6 @@
import java.io.IOException;
import java.lang.module.ModuleDescriptor;
import java.lang.module.ModuleDescriptor.Version;
-import java.lang.module.ModuleFinder;
-import java.lang.module.ModuleReference;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@@ -758,24 +756,16 @@
static String getDefaultAppVersion(Map<String, ? super Object> params) {
String appVersion = DEFAULT_VERSION;
- boolean hasModule = params.containsKey(MODULE.getID());
- if (hasModule) {
- List<Path> modulePath = MODULE_PATH.fetchFrom(params);
- if (!modulePath.isEmpty()) {
- ModuleFinder finder = ModuleFinder.of(modulePath.toArray(new Path[0]));
- String mainModule = JLinkBundlerHelper.getMainModule(params);
- Optional<ModuleReference> omref = finder.find(mainModule);
- if (omref.isPresent()) {
- ModuleDescriptor descriptor = omref.get().descriptor();
- Optional<Version> oversion = descriptor.version();
- if (oversion.isPresent()) {
- Log.verbose(MessageFormat.format(I18N.getString(
- "message.module-version"),
- oversion.get().toString(),
- mainModule));
- appVersion = oversion.get().toString();
- }
- }
+
+ ModuleDescriptor descriptor = JLinkBundlerHelper.getMainModuleDescription(params);
+ if (descriptor != null) {
+ Optional<Version> oversion = descriptor.version();
+ if (oversion.isPresent()) {
+ Log.verbose(MessageFormat.format(I18N.getString(
+ "message.module-version"),
+ oversion.get().toString(),
+ JLinkBundlerHelper.getMainModule(params)));
+ appVersion = oversion.get().toString();
}
}
--- a/src/jdk.jpackage/share/classes/jdk/jpackage/internal/resources/MainResources.properties Thu Sep 26 09:19:46 2019 -0400
+++ b/src/jdk.jpackage/share/classes/jdk/jpackage/internal/resources/MainResources.properties Thu Sep 26 10:37:37 2019 -0400
@@ -41,6 +41,7 @@
message.debug-working-directory=Kept working directory for debug: {0}
message.bundle-created=Succeeded in building {0} package
message.module-version=Using version "{0}" from module "{1}" as application version
+message.module-class=Using class "{0}" from module "{1}" as application main class
error.cannot-create-output-dir=Destination directory {0} cannot be created
error.cannot-write-to-output-dir=Destination directory {0} is not writable
--- a/src/jdk.jpackage/share/classes/jdk/jpackage/internal/resources/MainResources_ja.properties Thu Sep 26 09:19:46 2019 -0400
+++ b/src/jdk.jpackage/share/classes/jdk/jpackage/internal/resources/MainResources_ja.properties Thu Sep 26 10:37:37 2019 -0400
@@ -41,6 +41,7 @@
message.debug-working-directory=Kept working directory for debug: {0}
message.bundle-created=Succeeded in building {0} package
message.module-version=Using version "{0}" from module "{1}" as application version
+message.module-class=Using class "{0}" from module "{1}" as application main class
error.cannot-create-output-dir=Destination directory {0} cannot be created
error.cannot-write-to-output-dir=Destination directory {0} is not writable
--- a/src/jdk.jpackage/share/classes/jdk/jpackage/internal/resources/MainResources_zh_CN.properties Thu Sep 26 09:19:46 2019 -0400
+++ b/src/jdk.jpackage/share/classes/jdk/jpackage/internal/resources/MainResources_zh_CN.properties Thu Sep 26 10:37:37 2019 -0400
@@ -41,6 +41,7 @@
message.debug-working-directory=Kept working directory for debug: {0}
message.bundle-created=Succeeded in building {0} package
message.module-version=Using version "{0}" from module "{1}" as application version
+message.module-class=Using class "{0}" from module "{1}" as application main class
error.cannot-create-output-dir=Destination directory {0} cannot be created
error.cannot-write-to-output-dir=Destination directory {0} is not writable
--- a/test/jdk/tools/jpackage/helpers/JPackageHelper.java Thu Sep 26 09:19:46 2019 -0400
+++ b/test/jdk/tools/jpackage/helpers/JPackageHelper.java Thu Sep 26 10:37:37 2019 -0400
@@ -51,6 +51,24 @@
private static final Path JAR;
private static final Path JLINK;
+ public static class ModuleArgs {
+ private final String version;
+ private final String mainClass;
+
+ ModuleArgs(String version, String mainClass) {
+ this.version = version;
+ this.mainClass = mainClass;
+ }
+
+ public String getVersion() {
+ return version;
+ }
+
+ public String getMainClass() {
+ return mainClass;
+ }
+ }
+
static {
if (OS.startsWith("win")) {
JPACKAGE = BIN_DIR.resolve("jpackage.exe");
@@ -375,16 +393,16 @@
createModule("Hello.java", "input", "hello", null, true);
}
- public static void createHelloModule(String version) throws Exception {
- createModule("Hello.java", "input", "hello", version, true);
+ public static void createHelloModule(ModuleArgs moduleArgs) throws Exception {
+ createModule("Hello.java", "input", "hello", moduleArgs, true);
}
public static void createOtherModule() throws Exception {
createModule("Other.java", "input-other", "other", null, false);
}
- private static void createModule(String javaFile, String inputDir,
- String aName, String version, boolean createModularJar) throws Exception {
+ private static void createModule(String javaFile, String inputDir, String aName,
+ ModuleArgs moduleArgs, boolean createModularJar) throws Exception {
int retVal;
File input = new File(inputDir);
@@ -431,9 +449,16 @@
args.add("--create");
args.add("--file");
args.add(inputDir + File.separator + "com." + aName + ".jar");
- if (version != null) {
- args.add("--module-version");
- args.add(version);
+ if (moduleArgs != null) {
+ if (moduleArgs.getVersion() != null) {
+ args.add("--module-version");
+ args.add(moduleArgs.getVersion());
+ }
+
+ if (moduleArgs.getMainClass()!= null) {
+ args.add("--main-class");
+ args.add(moduleArgs.getMainClass());
+ }
}
args.add("-C");
args.add("module" + File.separator + "com." + aName);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/tools/jpackage/share/AppMainClassModuleTest.java Thu Sep 26 10:37:37 2019 -0400
@@ -0,0 +1,108 @@
+/*
+ * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.io.File;
+import java.nio.file.Files;
+
+/*
+ * @test
+ * @summary jpackage create image using main class from main module
+ * @library ../helpers
+ * @build JPackageHelper
+ * @build JPackagePath
+ * @modules jdk.jpackage
+ * @run main/othervm -Xmx512m AppMainClassModuleTest
+ */
+public class AppMainClassModuleTest {
+
+ private static final String OUTPUT = "output";
+ private static final String app = JPackagePath.getApp();
+ private static final String appOutput = JPackagePath.getAppOutputFile();
+
+ private static final String[] CMD = {
+ "--package-type", "app-image",
+ "--input", "input",
+ "--dest", OUTPUT,
+ "--name", "test",
+ "--module", "com.hello",
+ "--module-path", "input"
+ };
+
+ private static final String[] CMD_MAIN_CLASS = {
+ "--package-type", "app-image",
+ "--input", "input",
+ "--dest", OUTPUT,
+ "--name", "test",
+ "--module", "com.hello/com.hello.Hello",
+ "--module-path", "input"
+ };
+
+ private static void validate(String buildOutput) throws Exception {
+
+ File outfile = new File(appOutput);
+ int retVal = JPackageHelper.execute(outfile, app);
+ if (retVal != 0) {
+ throw new AssertionError(
+ "Test application exited with error: ");
+ }
+
+ if (!outfile.exists()) {
+ throw new AssertionError(appOutput + " was not created");
+ }
+ String output = Files.readString(outfile.toPath());
+ String[] result = output.split("\n");
+
+ if (!result[0].trim().equals("jpackage test application")) {
+ throw new AssertionError("Unexpected result[0]: " + result[0]);
+ }
+
+ if (!result[1].trim().equals("args.length: 0")) {
+ throw new AssertionError("Unexpected result[1]: " + result[1]);
+ }
+ }
+
+ private static void testMainClassFromModule() throws Exception {
+ JPackageHelper.createHelloModule(
+ new JPackageHelper.ModuleArgs(null, "com.hello.Hello"));
+
+ validate(JPackageHelper.executeCLI(true, CMD));
+ JPackageHelper.deleteOutputFolder(OUTPUT);
+ validate(JPackageHelper.executeToolProvider(true, CMD));
+ JPackageHelper.deleteOutputFolder(OUTPUT);
+ }
+
+ private static void testMainClassFromCLI() throws Exception {
+ JPackageHelper.createHelloModule(
+ new JPackageHelper.ModuleArgs(null, "com.hello.Hello2"));
+
+ validate(JPackageHelper.executeCLI(true, CMD_MAIN_CLASS));
+ JPackageHelper.deleteOutputFolder(OUTPUT);
+ validate(JPackageHelper.executeToolProvider(true, CMD_MAIN_CLASS));
+ }
+
+ public static void main(String[] args) throws Exception {
+ testMainClassFromModule();
+ testMainClassFromCLI();
+ }
+
+}
--- a/test/jdk/tools/jpackage/share/AppVersionModuleTest.java Thu Sep 26 09:19:46 2019 -0400
+++ b/test/jdk/tools/jpackage/share/AppVersionModuleTest.java Thu Sep 26 10:37:37 2019 -0400
@@ -45,8 +45,8 @@
"--dest", OUTPUT,
"--name", "test",
"--module", "com.hello/com.hello.Hello",
- "--module-path", "input",
- };
+ "--module-path", "input"
+ };
private static final String[] CMD_CLI_VERSION = {
"--package-type", "app-image",
@@ -55,7 +55,8 @@
"--name", "test",
"--module", "com.hello/com.hello.Hello",
"--module-path", "input",
- "--app-version", CLI_VERSION};
+ "--app-version", CLI_VERSION
+ };
private static void validate(String version)
throws Exception {
@@ -94,7 +95,8 @@
}
public static void main(String[] args) throws Exception {
- JPackageHelper.createHelloModule(MODULE_VERSION);
+ JPackageHelper.createHelloModule(
+ new JPackageHelper.ModuleArgs(MODULE_VERSION, null));
testVersion();
testVersionToolProvider();
}