JPackages JavaFX + Spring boot 无法启动

Dra*_*nna 3 java javafx spring-boot jpackage

我正在尝试使用 Spring Boot 构建 JavaFX 应用程序并使用 jpackage 进行部署。

使用该javafx-maven-plugin javafx:run命令时,我可以看到项目启动。但是将其构建为*.msi安装程序、安装并启动后*.exe,没有任何反应(无论是通过双击还是命令行运行)。

我该如何解决这个问题,或者至少知道发生了什么?我怀疑javafx:run目标是手动 jlink 中缺少一些参数,但是我无法使用,javafx:jlink因为应用程序本身不是模块,因为 Spring Boot 尚未模块化。

这是来源;

编辑: 根据@Slaw的建议,我修改了jpackage命令以包含`--win-console。从而成功运行该应用程序。虽然这是一个很好的步骤,但这不是我想要的,我想在没有提示的情况下运行它。

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.test</groupId>
    <artifactId>mytest</artifactId>
    <packaging>jar</packaging>
    <version>1.0</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.6.0</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.6.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.6.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>17.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-graphics</artifactId>
            <version>17.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>17.0.1</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.6.0</version>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.8</version>
                <configuration>
                    <mainClass>org.test.Main</mainClass>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>17</source>
                    <target>17</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>install</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
Run Code Online (Sandbox Code Playgroud)

主要的:

package org.test;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class Main extends Application {
    private ConfigurableApplicationContext springContext;
    private Parent rootNode = new AnchorPane();
    public static void main(String[] args) {
        Application.launch(args);
    }
    @Override
    public void start(Stage stage) throws Exception {
        stage.setScene(new Scene(rootNode, 700, 700));
        stage.setMinWidth(700);
        stage.setMinHeight(700);
        stage.show();
    }
    public void init() throws  Exception{
        springContext = SpringApplication.run(Main.class);
    }
    public void stop() throws Exception{
        springContext.close();
    }
}
Run Code Online (Sandbox Code Playgroud)

我的构建脚本是:

@ECHO OFF
SETLOCAL
call mvn clean install
set manual_modules=,javafx.controls,javafx.graphics,javafx.fxml
for /f %%i in ('jdeps --multi-release 17 --ignore-missing-deps --print-module-deps --class-path "target/lib/*" target/classes/org/test/Main.class') do set detected_modules=%%i
echo jlink
jlink --no-header-files --no-man-pages --compress=2 --strip-debug --module-path "target/lib" --add-modules %detected_modules%%manual_modules% --output target/java-runtime
echo jpackage
jpackage --type msi --dest target/installer --input target --name mytest --main-class org.test.Main --main-jar mytest-1.0.jar --runtime-image target/java-runtime
endlocal
@ECHO ON
Run Code Online (Sandbox Code Playgroud)

Dra*_*nna 6

我没有解释为什么第一个jpackage命令可以使用--win-console而不能使用。我不明白的事情一定在幕后发生(如果有人知道的话,我仍然对这个答案感兴趣!)。

无论如何,exe经过一些调试和多次尝试,我成功地制作了一个。

  1. 通过 Spring Boot Loader 启动应用程序,这意味着jpackage现在的命令是: jpackage --type msi --dest output/installer --input target --name testing --main-class org.springframework.boot.loader.PropertiesLauncher --main-jar mytest-1.0.jar --runtime-image target/java-runtime
  2. 添加 spring boot loader 依赖pom.xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-loader</artifactId>
    <version>2.6.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
  1. 修改spring-boot-maven-plugin配置:
           <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.6.0</version>
                <configuration>
                    <mainClass>org.test.Main</mainClass>
                    <layout>ZIP</layout>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
Run Code Online (Sandbox Code Playgroud)

我承认我在这里所做的事情是相当实验性的(至少对我来说),所有可以提供更深入的了解、参考或更好方法的答案都将受到欢迎。

  • 这是多么宝贵的帮助啊。非常感谢你。 (2认同)