Gluon 编译 JavaFx -> exe

Gluon 编译 JavaFx -> exe

能力强的伙伴可以直接参考官方文档

需要注意的问题说在前面

写这篇文章是为了将自己踩过的坑和熬过的夜以经验的方式分享出来,给对这方面技术也感兴趣的同志们一点点参考价值,希望这个技术栈的文章越来越多吧。

java AOT 编译 区别于 jlink编译

mvn gluonfx:runagent 首次编译的时候这个必须运行,主要是自动生成graalvm需要的反射配置,因此要确保程序里所有有反射的地方都要跑到。运行完毕后会生成这样几个文件

mvn gluonfx:build ,这是编译命令,运行的时间较长,运行完毕后,会在 target\gluonfx\x86_64-windows 目录下生成对应的 exe文件,就是最终的可以执行程序

mvn gluonfx:nativerun ,这个命令运行exe文件,与直接运行exe不同,通过此命令运行能在控制台打印出堆栈信息,用于排查问题

windows exe编译的过程涉及的环境的搭建较为复杂,如果只想构建apk的同志,请参考文章 Gluon 编译 JavaFx -> android apk

开发工具

  • idea 2023.3
  • idea gluon plugin
  • git
  • apache-maven-3.8.4

环境准备

vs 2022的安装明细

(来自官网文档/platforms/windows这一节

可以参考我的安装明细

以上步骤之后,新增一个路径到Path环境变量中

(因为后续编译的时候,会用到这个路径下的cl,默认没有添加到path,下面的版本号 14.29.30133根据自己的安装情况设置)

C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.29.30133\bin\HostX86\x86

配置上jdk环境变量

# 新增环境变量
JAVA_HOME=D:\development\env\java\openjdk-21.0.2
CLASSPATH=.;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar
# Path新增条目
%JAVA_HOME%\bin
%JAVA_HOME%\jre\bin

安装GraalVM CE Gluon 22.1.0.1-Final

下载解压到合适的目录后配置好环境变量

GRAALVM_HOME=D:\development\env\java\graalvm-svm-java17-windows-gluon-22.1.0.1-Final

编译官网示例项目

(到这里,我默认你的环境已经安装了git、mvn等工具,并配置相应的环境变量,且以上的步骤都没有问题)

拉取项目到本地

git clone https://github.com/gluonhq/gluon-samples.git

使用idea打开项目,设置项目的jdk为17+,给文件 gluon-samples/HelloFX/pom.xml文件增加几个build配置项

<properties>
    <main.class>hellofx.HelloFX</main.class>
    <gluonfx.target>host</gluonfx.target>
    <gluonfx.maven.plugin.version>1.0.23</gluonfx.maven.plugin.version>
    <javafx.maven.plugin.version>0.0.8</javafx.maven.plugin.version>
</properties>
<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>17</source>
                    <target>17</target>
                    <release>17</release>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>${javafx.maven.plugin.version}</version>
                <configuration>
                    <mainClass>${main.class}</mainClass>
                </configuration>
            </plugin>

            <plugin>
                <groupId>com.gluonhq</groupId>
                <artifactId>gluonfx-maven-plugin</artifactId>
                <version>${gluonfx.maven.plugin.version}</version>
                <configuration>
                     <target>${gluonfx.target}</target>
                     <mainClass>${main.class}</mainClass>
                    <reflectionList>
                        <list>.*\\.db$</list>
                        <list>.*\\.xlsx$</list>
                    </reflectionList>
                </configuration>
            </plugin>
        </plugins>
    </build>

新建build.bat文件

在项目路径 gluon-samples/HelloFX新建一个build.bat文件

call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat"
mvn  gluonfx:build -DskipTests=true -P desktop

执行编译

cd 项目路径
./build.bat

查看结果

编译结果输出到了 gluon-samples\HelloFX\target\gluonfx\x86_64-windows

异常处理

1.java.io.IOException: Cannot run program "cl" (in directory "D:\workspace\code\mycode\Gluon\gluon-samples\HelloFX\target\gluonfx\x86_64-windows\gvm\HelloFX"): CreateProcess error=2, 系统找不到指定的文件。

出现这个异常是因为上面的cl指令路径没有添加到path环境变量中

2.java.lang.IllegalArgumentException: We currently can't compile to aarch64-linux-android when running on x86_64-microsoft-windows

这个异常是编译在x86_64的环境中编译aarch64-linux-android,我们搭建的环境只能编译exe,导致这个错误的原因是项目的profiles设置如下

而且是直接到

这里面去执行的编译,在这里执行没有预先执行vcvars64.bat,这也是前面写那个build脚本的原因

如果想要构建apk,请参考文章 Gluon 编译 JavaFx -> android apk

修复方法就是将项目profiles选择为desktop,同时使用脚本去执行。

3.exe复制到其他机器执行不了,没有任何反应

我排查下来发现是其他机器配置了一个jdk环境,使用的java版本和我编译的版本有差别,运行的时候出现错误,但是没搞懂为啥没有任何提示信息,将其他机器的java_home 环境变量失效化,程序就正常运行了,这里可以临时写一下bat脚本,将java_home改成其他路径

将desktop编译动作绑定packge到生命周期

这样会将build脚本执行和package进行捆绑

<properties>
    <skip.exec>true</skip.exec>
</properties>

<profiles>
    <profile>
        <id>ios</id>
        <properties>
            <gluonfx.target>ios</gluonfx.target>
        </properties>
    </profile>
    <profile>
        <id>android</id>
        <properties>
            <gluonfx.target>android</gluonfx.target>
        </properties>
    </profile>
    <profile>
        <id>desktop</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <env>desktop</env>
            <skip.exec>false</skip.exec>
            <gluonfx.target>host</gluonfx.target>
        </properties>
    </profile>
</profiles>
<build>
    <plugins>
        <!-- Exec Maven 插件 -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
                <execution>
                    <id>run-prepare-env</id>
                    <phase>package</phase>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                    <configuration>
                        <executable>./bin/build.bat</executable>
                        <arguments>
                        </arguments>
                    </configuration>
                </execution>
            </executions>
            <configuration>
                <skip>${skip.exec}</skip>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
        </plugin>

        <plugin>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-maven-plugin</artifactId>
            <version>${javafx.plugin.version}</version>
            <configuration>
                <mainClass>${mainClassName}</mainClass>
            </configuration>
        </plugin>

        <plugin>
            <groupId>com.gluonhq</groupId>
            <artifactId>gluonfx-maven-plugin</artifactId>
            <version>${gluonfx.plugin.version}</version>
            <configuration>
                <target>${gluonfx.target}</target>
                <attachList>
                    <list>display</list>
                    <list>lifecycle</list>
                    <list>statusbar</list>
                    <list>storage</list>
                </attachList>
                <reflectionList>
                    <list>com.xxx.PrimaryPresenter</list>
                    <list>com.xxx.SecondaryPresenter</list>
                </reflectionList>
                <mainClass>${mainClassName}</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

posted @ 2024-08-31 11:13  南怪布德  阅读(69)  评论(0编辑  收藏  举报