java做桌面程序,有没有什么优雅的方式集成jre?

因为工作需要,转移一部分服务上业务代码,做成桌面小工具。自用开发机器上有jre,运行起来没问题。但是测试同事机器上不装jre,想让他们使用就比较麻烦。
有什么优雅的方式,能够让java的桌面程序运行起来。

阅读 2.3k
2 个回答

你们用的javafx吗,javafx的话,jdk使用graalvm,再配合gluonfx-maven-plugin这个maven插件,可以把项目打包成不需要jre的exe文件。

我最近也是在了解这个,下边是我的pom文件配置

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>hsb.javafx</groupId>
    <artifactId>javafx-learn</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>javafx-learn</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <junit.version>5.8.2</junit.version>
        <mainclass.custom>hsb.javafx.javafxlearn.Main</mainclass.custom>
        <javafxmainclass.custom>hsb.javafx.javafxlearn.HelloApplication</javafxmainclass.custom>
        <gluonfx.maven.plugin.version.custom>1.0.15</gluonfx.maven.plugin.version.custom>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>17.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>17.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.kordamp.bootstrapfx</groupId>
            <artifactId>bootstrapfx-core</artifactId>
            <version>0.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.10.1</version>
                <configuration>
                    <source>17</source>
                    <target>17</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.8</version>
                <executions>
                    <execution>
                        <!-- Default configuration for running with: mvn clean javafx:run -->
                        <id>default-cli</id>
                        <configuration>
                            <mainClass>hsb.javafx.javafxlearn/hsb.javafx.javafxlearn.HelloApplication</mainClass>
                            <launcher>app</launcher>
                            <jlinkZipName>app</jlinkZipName>
                            <jlinkImageName>app</jlinkImageName>
                            <noManPages>true</noManPages>
                            <stripDebug>true</stripDebug>
                            <noHeaderFiles>true</noHeaderFiles>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!-- 此插件必须放在父 POM 中  -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <!--
                            设置将依赖整合到一个自定义名称的 JAR 包。
                            如果不设置此项,依赖将整合到 Maven 无插件时的默认 JAR 包,并生成一个前缀为 original- 的无依赖 JAR 包
                             -->
                            <shadedArtifactAttached>true</shadedArtifactAttached>
                            <!-- 设置生成的 JAR 包后缀名 -->
                            <shadedClassifierName>shaded-with-dependencies</shadedClassifierName>
                            <!-- 设置程序运行入口 -->
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>${mainclass.custom}</mainClass>
                                </transformer>
                            </transformers>
                            <!-- 设置 JAR 包输出目录 -->
                            <outputDirectory>${project.build.directory}/#maven-shade-plugin</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>


            <plugin>
                <groupId>com.gluonhq</groupId>
                <artifactId>gluonfx-maven-plugin</artifactId>
                <version>${gluonfx.maven.plugin.version.custom}</version>
                <configuration>
                    <target>host</target>
                    <mainClass>${javafxmainclass.custom}</mainClass>
                    <reflectionList>
                        <list>org.wangpai.demo.purejavafx.MainFace</list>
                    </reflectionList>
                    <graalvmHome>D:\kaifa_environment\jdk\graalvm-ee-java17-22.3.0</graalvmHome>
                </configuration>
            </plugin>

        </plugins>
    </build>
</project>

graalvm打包处原生镜像还需要一些工具,比如需要visiual studio。

我使用的打包命令 .cmd文件

call "D:\kaifa_environment\visiual studio\2022\VC\Auxiliary\Build\vcvars64.bat"

mvn clean gluonfx:build
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题