如何为JLink启动器可执行文件设置虚拟机选项

9
当使用 jlink 时,会生成一个 bin/java 文件。这个可执行文件可以通过在命令行上指定选项来接受虚拟机参数,就像平常一样(例如 -Dsystem.property=value-Xmx1G)。 jlink 还提供了一个 --launcher 选项,用于创建一个可直接运行的可执行文件,而不必通过模块名来调用 bin/java 可执行文件。
如何使启动器可执行文件预配置为使用我选择的 JVM 选项?
2个回答

3
你可以使用 add-options jlink 插件。
例如,如果你想设置 Xmx:
jlink --add-options="-Xmx100m" ...

要查看jlink插件列表,请运行jlink --list-pluginsadd-options插件当前(JDK14)的文档如下:
Plugin Name: add-options
Option: --add-options=<options>
Description: Prepend the specified <options> string, which may include
whitespace, before any other options when invoking the virtual machine
in the resulting image.

请注意,一些插件显然是不稳定的(包括add-options):https://docs.oracle.com/en/java/javase/12/tools/jlink.html


1

有一种或两种方法可以解决这个问题,但我主要会集中在默认的Java方法上。

实际答案 - 使用JPackage。JLink只是运行时的映像。JPackage是您的可分发文件。

Support for native packaging formats to give the end user a more natural installation experience. Specifically, the tool will support the following formats:

    Windows: msi, exe
    macOS: pkg, app in a dmg (drag the app into the Applications directory)
    Linux: deb, rpm

The application will be installed in the typical default directory for each platform unless the end-user specifies an alternate directory during the installation process (for example, on Linux the default directory will be /opt).

The ability to specify JDK and application arguments at packaging time that will be used when launching the application

The ability to package applications in ways that integrate into the native platform, for example:

    Setting file associations to allow launching an application when a file with an associated suffix is opened
    Launching from a platform-specific menu group, such as Start menu items on Windows
    Option to specify update rules for installable packages (such as in rpm/deb)


1) - 指定一个 @Args 文件

您可以创建一个 @args 文件,并将其与 jlink 应用程序捆绑在一起部署,然后在启动应用程序时引用它。

java @args -m module/main

2) 使用新的环境变量

JDK_JAVA_OPTIONS=--add-opens java.base/java.lang=...... -Xmx1G -Djdk.logging.provider=

https://docs.oracle.com/javase/9/tools/java.htm#JSWOR624

3)使用JLink/JMod在模块中指定主类

https://maven.apache.org/plugins/maven-jmod-plugin/plugin-info.html

      <plugin>
        <artifactId>maven-jmod-plugin</artifactId>
        <version>3.0.0-alpha-1</version>
        <extensions>true</extensions>
        <configuration>
          <module>
          <mainClass>mainClass</mainClass>
        </configuration>
      </plugin>

4) 使用JLink创建自定义启动器/编辑JDK_VM_OPTIONS

<plugin>
                        <artifactId>maven-jlink-plugin</artifactId>
                        <version>3.0.0-alpha-2-SNAPSHOT</version>
                        <extensions>true</extensions>
                        <configuration>
                            <noHeaderFiles>true</noHeaderFiles>
                            <noManPages>true</noManPages>
                            <stripDebug>true</stripDebug>
                            <verbose>true</verbose>
                            <compress>2</compress>
                            <launcher>customjrelauncher=module/mainClass</launcher>
                        </configuration>
                        <dependencies>
                            <dependency>
                                <groupId>org.ow2.asm</groupId>
                                <artifactId>asm</artifactId>
                                <version>${maven.asm.version}</version>
                            </dependency>
                        </dependencies>
                    </plugin>

在生成的 .sh/.bat 文件中,有一个变量用于指定任何自定义插件。
如果您正在使用 moditect,则可以在您的 module-info 中使用它来指定主类描述符。
<plugin>
                <groupId>org.moditect</groupId>
                <artifactId>moditect-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>add-module-infos</id>
                        <phase>package</phase>
                        <goals>
                            <goal>add-module-info</goal>
                        </goals>
                        <configuration>
                            <overwriteExistingFiles>true</overwriteExistingFiles>
                            <module>
                                <mainClass>mainClassLocation</mainClass>
                            </module>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

如果您查看第二个解决方案,您将会看到VM参数|JDK_JAVA_OPTIONS=--add-opens java.base/java.lang=...... -Xmx1G -Djdk.logging.provider=.请记得标记为答案。 - Marc Magon
1
不幸的是,第二个答案并不好,因为它意味着某人不能只需双击可执行文件并使用开发人员的首选JVM选项运行它。这就是我在问题中所说的“预配置”的意思,如果这不清楚,对不起。 - knaccc
1
我并不否认设置环境变量是传递JVM参数的一种方式。但我想说的是,对于那些需要一个可执行文件而又不知道什么是环境变量或如何设置它的人来说,这并不是一个有用的答案。 - knaccc
1
我同意@knaccc的观点,这根本不是一个答案。问题非常简单:如何使Java可执行文件始终使用特定选项运行。这并没有回答这个问题。对于最终用户来说,设置环境变量绝对不是一个好的选择。 - Renato
要始终运行,请使用Java环境变量。它就是为此而存在的。感谢您抽出时间尝试它。 - Marc Magon
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接