簡體   English   中英

如何為 JLink 啟動器可執行文件設置 VM 選項

[英]How to set VM options for JLink launcher executable

使用jlink ,會生成一個bin/java文件。 此可執行文件將通過在命令行上以通常的方式指定選項(例如-Dsystem.property=value-Xmx1G )來接受 VM 選項。

jlink還提供了一種--launcher選項來創建,而不必調用可以直接運行可執行文件, bin/java用模塊名字的可執行文件。

如何使啟動器可執行文件預先配置為使用我選擇的 JVM 選項?

您可以使用add-options jlink 插件。

例如,如果要設置 Xmx:

jlink --add-options="-Xmx100m" ...

要查看 jlink 插件列表,請運行jlink --list-plugins

add-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.

請注意,某些插件顯然不穩定(包括添加選項): https : //docs.oracle.com/en/java/javase/12/tools/jlink.html

有一種或兩種方法可以解決這個問題,但主要是我將專注於默認的 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 在您的模塊信息中指定主類描述符:

<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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM