Gluon 编译 JavaFx -> exe
  JnAQjEG0hENE 19天前 129 0

Gluon 编译 JavaFx -> exe

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

开发工具

  • 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脚本的原因

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

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

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 19天前 0

暂无评论

推荐阅读
  VGxawBTN4xmE   13小时前   10   0   0 Java
  FHUfYd9S4EP5   3天前   24   0   0 Java
  sSh3rBaiME5f   4天前   32   0   0 Java
  qCe06rFCa8NK   3天前   18   0   0 Java
  ZTo294hNoDcA   3天前   26   0   0 Java
  FHUfYd9S4EP5   3天前   20   0   0 Java
  QGiRIwDaZAe8   4天前   23   0   0 Java
JnAQjEG0hENE
作者其他文章 更多