GraalVM详细安装及打包springboot、java、javafx使用教程(打包javafx项目篇)

前言

在当前多元化开发环境下,Java作为一种广泛应用的编程语言,其应用部署效率与灵活性的重要性日益凸显。Spring Boot框架以其简洁的配置和强大的功能深受开发者喜爱,而JavaFX则为开发者提供了构建丰富桌面客户端应用的能力。然而,将Spring Boot后端服务与JavaFX前端界面整合并实现高效部署,对许多开发者来说是一项挑战。

GraalVM的出现,恰好解决了这一难题。作为Oracle公司推出的一款高性能运行时环境,它支持多种语言运行,并能将Java应用编译为原生可执行文件,从而大大提升了应用的启动速度和资源利用率。通过利用GraalVM的原生镜像生成工具(Substrate VM),我们可以将Spring Boot应用与JavaFX项目无缝集成,并将其打包为独立的、跨平台的可执行程序。

本教程将深入浅出地为您介绍如何详细安装GraalVM,以及如何在此基础上,将普通的Java项目进行打包,以实现高效便捷的应用部署。我们将逐步解析操作步骤,助您掌握这一技术,提升项目的部署效率和用户体验。

1. 环境搭建

上一篇:GraalVM详细安装及打包springboot、java、javafx使用教程(打包普通JAVA项目篇)
下一篇:GraalVM详细安装及打包springboot、java、javafx使用教程(打包springboot2篇)

2.项目搭建

1.新建java项目

idea–>file—>new—>project填写好相关信息,选择JDK17
在这里插入图片描述

在这里插入图片描述

2.完整项目结构

在这里插入图片描述

3.导入依赖文件

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>17</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <javafx.version>17.0.2</javafx.version>
        <javafx.staticSdk.version>21-ea+5</javafx.staticSdk.version>
        <gluonfx-maven-plugin.version>1.0.18</gluonfx-maven-plugin.version>
        <start-class>com.star.graalvm.AppStart</start-class>
    </properties>

依赖

<dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>${javafx.version}</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>${javafx.version}</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-media</artifactId>
            <version>${javafx.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

导入编译打包插件

 <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <release>${java.version}</release>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.8</version>
                <configuration>
                    <mainClass>${start-class}</mainClass>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.gluonhq</groupId>
                <artifactId>gluonfx-maven-plugin</artifactId>
                <version>${gluonfx-maven-plugin.version}</version>
                <configuration>
                    <!--运行主类-->
                    <mainClass>${start-class}</mainClass>
                    <!--Java11 静态库的版本Default: 11-ea+10-->
                    <!--
                    <javaStaticSdkVersion>11-ea+10</javaStaticSdkVersion>
                    -->
                    <!--Java17 静态库的版本21-ea+5 -->
                    <javafxStaticSdkVersion>${javafx.staticSdk.version}</javafxStaticSdkVersion>
                    <!-- 允许的资源文件后缀 -->
                    <resourcesList>
                        <list>.*\\.properties$</list>
                        <list>.*\\.yml$</list>
                        <list>.*\\.dll</list>
                    </resourcesList>
                    <!-- 涉及反射的类 -->
                    <reflectionList>
                        <list>com.star.graalvm.App</list>
                        <list>com.star.graalvm.control.HomeControl</list>
                    </reflectionList>
                    <nativeImageArgs>
                        <!-- “快速构建”模式,以减少在开发过程中生成本机可执行文件所需的时间。请注意,此模式可能会对峰值性能和生成的可执行文件的大小产生负面影响,因此建议仅用于开发目的。 -->
                        <arg>--add-exports=org.graalvm.nativeimage.builder/com.oracle.svm.core.jdk=ALL-UNNAMED</arg>
                    </nativeImageArgs>
                    <releaseConfiguration>
                        <!-- all targets -->
                        <!--<packageType></packageType>-->
                        <description>graalvm-javafx</description>
                        <vendor>shin</vendor>
                        <version>${version}</version>
                    </releaseConfiguration>
                    <runtimeArgs>
                        <arg>-Dfile.encoding=UTF-8</arg>
                    </runtimeArgs>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>build</goal>
                        </goals>
                        <phase>package</phase>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

4.新建java类

1.启动类
package com.star.graalvm;

import com.star.graalvm.conf.AppConfig;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Screen;
import javafx.stage.Stage;


import java.util.regex.Pattern;

/**
 * @create 2023-09
 * @author lstar
 * @description: 主程序
 */
public class App extends Application {

    public static void main(String[] args) {
        // 解决Linux上编译为native-image时运行错误:
        String osName = System.getProperty("os.name", "");
        if (Pattern.matches("Linux.*", osName)) {
            System.setProperty("prism.forceGPU", "true");
        }

        AppConfig.init();
        launch(args);
    }

    @Override
    public void init() throws Exception {
        super.init();
        // 设置系统屏幕缩放比例
        try {
            var scaleX =  Screen.getScreens().get(0).getOutputScaleX();
            System.setProperty("glass.win.uiScale", String.valueOf(scaleX));
        } catch (Exception ignored) {
            System.setProperty("glass.win.uiScale", "1.0");
        }
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        // 加载并创建主场景
        Parent root = FXMLLoader.load(getClass().getResource("/fxml/Home.fxml"));
        Scene scene = new Scene(root, AppConfig.stageWidth, AppConfig.stageHeight);
        // 设置窗口信息
        primaryStage.setTitle(AppConfig.title);
        primaryStage.setResizable(AppConfig.stageResizable);
        primaryStage.getIcons().add(new Image(App.class.getResourceAsStream(AppConfig.icon)));
        primaryStage.setScene(scene);
        primaryStage.show();

    }


    @Override
    public void stop() throws Exception {
        System.out.println("stop");
        super.stop();
    }
}

java11以上已经移除了javafx相关组件之前运行启动类是无法运行的

package com.star.graalvm;

import javafx.application.Application;

/**
 * @create 2023-09
 * @author lstar
 * @description: 启动类
 */
public class AppStart
{
    public static void main( String[] args )
    {
        Application.launch(App.class);
    }
}

2.视图控制类
package com.star.graalvm.control;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.stage.FileChooser;
import javafx.stage.Window;

import java.io.File;
import java.util.Arrays;

/**
 * @create 2023-09
 * @author lstar
 * @description: 视图控制类
 */
public class HomeControl {

    @FXML
    public Button alterBtn;
    @FXML
    public Button fileBtn;
    @FXML
    public TextField fileText;
    @FXML
    public AnchorPane rootPane;

    @FXML
    public ImageView imgView;

    @FXML
    public void initialize() {
        System.out.println("init");
    }

    /**
     * 文件按钮单击事件
     */
    @FXML
    public void fileBtnClick(MouseEvent actionEvent) {

        Window window = rootPane.getScene().getWindow();
        FileChooser fc = new FileChooser();
        //设置选择框的左上角标题
        fc.setTitle("单文件选择");
        //设置文件初始化打开路径
        fc.setInitialDirectory(new File("D:" + File.separator));
        //设置文件的选择类型
        fc.getExtensionFilters().addAll(
                new FileChooser.ExtensionFilter("图片类型", "*.jpg", "*.png", "*.gif"),
                new FileChooser.ExtensionFilter("文本类型", "*.txt", "*.java", "*.doc", "*.docx", "*.xlx", "*.xlsx", "*.fxml"),
                new FileChooser.ExtensionFilter("所有类型", "*.*")
        );
        //文件显示框 选择的文件返回一个file
        File file = fc.showOpenDialog(window);
        String fileName = file == null ? "" : file.getName();
        String fileAbsolutePath = file == null ? "" : file.getAbsolutePath();
        if (file != null) {
            fileText.setText("文件名:" + fileName+"========" + "文件路径:" + fileAbsolutePath);

            if(isImageFile(file)){
             imgView.setImage(new Image(fileAbsolutePath));
            }
        }
    }

    /**
     * 判断文件后缀
     * @param file
     * @return
     */
    public static boolean isImageFile(File file) {
        if (file == null || !file.exists()) {
            return false;
        }

        String fileName = file.getName();
        int dotIndex = fileName.lastIndexOf(".");
        if (dotIndex <= 0 || dotIndex == fileName.length() - 1) {
            // 没有后缀名或者点在文件名末尾,都不是图片
            return false;
        }

        String extension = fileName.substring(dotIndex + 1).toLowerCase(); // 获取小写后缀名
        return Arrays.asList("jpg", "jpeg", "png", "gif").contains(extension);
    }
    /**
     * 弹出框按钮单击事件
     */
    @FXML
    public void alterBtnClick(MouseEvent actionEvent) {

        String javaVersion = System.getProperty("java.version");
        String javafxVersion = System.getProperty("javafx.version");
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setHeaderText("Hello Graalvm");
        alert.setContentText("Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + ".");
        alert.show();


    }

}

3. 应用配置类
package com.star.graalvm.conf;

import com.star.graalvm.App;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @create 2023-09
 * @author lstar
 * @description: 应用配置文件
 */
public class AppConfig {

    /**
     * 应用标题
     */
    public static String title = "JavaFx Test Application";
    /**
     * 应用图标
     */
    public static String icon = "/icon/icon.png";
    /**
     * 窗口宽度
     */
    public static int stageWidth = 600;
    /**
     * 窗口高度
     */
    public static int stageHeight = 600;
    /**
     * 允许调整窗口尺寸
     */
    public static boolean stageResizable = true;

    public static void init() {
        try {
            Properties properties = new Properties();
            InputStream in = App.class.getResourceAsStream("app.properties");
            properties.load(in);
            title = properties.getProperty("title");
            icon = properties.getProperty("icon");
            stageWidth = Integer.parseInt(properties.getProperty("stage.width"));
            stageHeight = Integer.parseInt(properties.getProperty("stage.height"));
            stageResizable = Boolean.parseBoolean(properties.getProperty("stage.resizable"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

app.properties 属性配置文件放在resources目录下

title=GraalVM & JavaFx Test Application
icon=icon/icon.png
stage.width=600
stage.height=600
stage.resizable=true
4.Home.fxml文件
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>

<AnchorPane fx:id="rootPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="600.0" stylesheets="@../css/Home.css" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.star.graalvm.control.HomeControl">
   <children>
      <Button fx:id="alterBtn" layoutX="74.0" layoutY="112.0" mnemonicParsing="false" onMouseClicked="#alterBtnClick" prefHeight="28.0" prefWidth="68.0" text="弹窗" />
      <Button fx:id="fileBtn" layoutX="441.0" layoutY="112.0" mnemonicParsing="false" onMouseClicked="#fileBtnClick" prefHeight="28.0" prefWidth="68.0" text="文件选择" />
      <TextField fx:id="fileText" layoutX="86.0" layoutY="170.0" prefHeight="43.0" prefWidth="429.0" promptText="文件地址" />
      <ImageView fx:id="imgView" fitHeight="290.0" fitWidth="429.0" layoutX="89.0" layoutY="242.0" pickOnBounds="true" preserveRatio="true" />
   </children>
</AnchorPane>

5.css文件
#rootPane{
    /*背景颜色*/
    -fx-background-color: #b6e3c1;
}

3.构建方式exe

执行完成后exe和jar文件在target—>gluonfx—>x86_64windows下

1.方式一:命令行

使用idea的terminal命令行进入项目目录,使用mvn命令构建

mvn clean
mvn gluonfx:run
mvn gluonfx:runagent
mvn gluonfx:build

2.方式二:maven插件

使用idea的maven插件打包idea—>maven—>当前项目—>plugins—>gluonfx---->gluonfx:run—>gluonfx:runagent—gluonfx:build>

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4.示例代码

  1. gitee
  2. github

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/478888.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

ANDRAXv6软件工具列表介绍

系统环境&#xff1a;Xiaomi HyperOS Android 14 以下列表仅为部分内容&#xff0c;实际工具更多&#xff0c;方便大家了解为什么ANDRAX比NetHunter更强大 注意以下内容&#xff1a;仅供学习&#xff0c;未经相关部门许可请勿随意测试或恶意破坏公共网络设备&#xff0c;违者…

工控机在机器人领域的应用丨工业一体机的应用

随着机器人技术的不断发展&#xff0c;机器人在制造、物流等领域得到了广泛应用。而工业控制计算机&#xff08;工控机&#xff09;作为机器人控制系统的核心设备&#xff0c;也在机器人领域发挥着越来越重要的作用。 机器人控制系统是机器人的核心部分&#xff0c;控制系统的…

基于Springboot的农产品销售管理系统+数据库+免费远程调试

项目介绍: 基于Springboot的农产品销售管理系统。Javaee项目&#xff0c;springboot项目。采用M&#xff08;model&#xff09;V&#xff08;view&#xff09;C&#xff08;controller&#xff09;三层体系结构&#xff0c;通过SpringMvc SpringBootMybatisVuemaven来实现。MyS…

Uscrapper:一款功能强大的网络资源爬取工具

关于Uscrapper Uscrapper是一款功能强大的网络资源爬取工具&#xff0c;该工具可以帮助广大研究人员从各种网络资源中轻松高效地提取出有价值的数据&#xff0c;并且提供了稳定、友好且易于使用的UI界面&#xff0c;是安全研究人员和网络分析人员的强有力工具。 Uscrapper最大…

PR如何制作火焰特效?VFX火焰动画元素PR视频剪辑素材

如何使用Premiere软件制作火焰特效动画&#xff1f;VFX火焰特效元素动画pr视频剪辑素材。使用颜色控制器轻松自定义辉光效果的颜色和强度。每个场景都充满逼真的火焰。在预告片、极限运动视频或任何需要电影火力的场景中添加动作的好方法。 来自&#xff1a;pr素材库&#xff0…

python云上水果超市的设计与实现flask-django-php-nodejs

伴随着我国社会的发展&#xff0c;人民生活质量日益提高。于是对云上水果超市进行规范而严格是十分有必要的&#xff0c;所以许许多多的信息管理系统应运而生。此时单靠人力应对这些事务就显得有些力不从心了。所以本论文将设计一套云上水果超市&#xff0c;帮助商家进行商品信…

仿京东项目——京西商城(数据库设计)

文章目录 仿京东——京西商城数据库设计建立E-R图数据库表设计用户表商品表订单表订单详情表评论表购物车表购物车项表 仿京东——京西商城 数据库设计 主要实体有&#xff1a; 用户 用户ID&#xff08;User_ID&#xff09;&#xff1a;唯一标识用户的主键 用户名&#xff0…

RuoYi-Vue-Plus(基础知识点jackson、mybatisplus、redis)

一、JacksonConfig 全局序列化反序列化配置 1.1yml中配置 #时区 spring.jackson.time-zoneGMT8 #日期格式 spring.jackson.date-formatyyyy-MM-dd HH:mm:ss #默认转json的属性&#xff0c;这里设置为非空才转json spring.jackson.default-property-inclusionnon_null #设置属性…

数据库级别的刷新和数据表级别的刷新结果不一样吗

这个刷新和这个刷新有啥区别 我发现点左边的刷新没有办法刷新出新注册成功的用户&#xff0c;但是下一张图片就能刷新出来 我也知不道是不是&#xff0c;还是我navicat有问题 是的&#xff0c;数据库级别的刷新和数据表级别的刷新的结果是不同的&#xff1a; 数据库级别的刷新…

JavaSE(简介)

1. Java语言特性 简单 Java语法是C语法的一个“纯净版本”&#xff0c;相当于对C做了一个减法。这里没有头文件、指针运算&#xff08;甚至指针 语法&#xff09;、结构、联合、操作符重载、虚基类等等。不仅如此&#xff0c;Java开发环境远远超出大多数其他编程语言的开 发环…

ttkbootstrap界面美化系列之按钮Button(三)

目录 一&#xff1a;Button接口 二&#xff1a;Button创建 三&#xff1a;Button主题 四&#xff1a;Button样式 五&#xff1a;Button状态 从本章开始将详细介绍ttkbootstrap中支持的常用组件&#xff0c;从按钮BUTTON开始&#xff0c;在各类界面设计中按钮几乎是必不可少…

解锁AI生成模型的无限可能:Stability-AI 带你领略前沿科技

厌倦了千篇一律的图片和视频&#xff1f;想要创作独一无二的艺术作品&#xff1f;Stability-AI 横空出世&#xff0c;为你打开通往 AI 生成模型的大门&#xff0c;带你领略前沿科技的无限可能&#xff01; 神奇的功能&#xff0c;尽在掌握 Stability-AI 拥有众多令人惊叹的功能…

第2讲-Memory(4)拓展

存储器芯片结构 存储器扩展 地址总线:这决定了处理器将从内存中读取数据或写入数据的内存位置。 数据总线:它包含已从内存位置读取或将写入内存位置的数据内容。 控制总线:它管理组件之间的信息流,指示操作是读取还是写入,并确保操作在正确的时间发生。

聊一下大模型的函数调用-Function call

你好&#xff0c;我是 shengjk1&#xff0c;多年大厂经验&#xff0c;努力构建 通俗易懂的、好玩的编程语言教程。 欢迎关注&#xff01;你会有如下收益&#xff1a; 了解大厂经验拥有和大厂相匹配的技术等 希望看什么&#xff0c;评论或者私信告诉我&#xff01; 文章目录 一…

Springboot 利用自定义注解+切面,实现 查询数据集合时主动加序列字段

利用自定义注解切面,实现 查询数据集合时主动加序列字段, 只需要在Dao接口&#xff0c;方法上引入注解即可 import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotat…

数据结构 - 链表

一.链表的概念 链表是一个在物理存储单元中不连续&#xff0c;没有顺序的的存储结构&#xff0c;关于它的顺序是由链表中的指针链接实现的&#xff0c;是一种递归的数据结构&#xff0c;链表有一系列节点组成&#xff0c;而这些节点会在运行时动态生成。 节点包括两个部分&am…

【docker】查看并拷贝容器内文件

一、查询容器 查询所有容器 docker ps查询名为os11的容器 docker ps | grep os11查询名为os11的容器&#xff08;包含不运行的&#xff09; docker ps -a| grep os11 docker ps [option] 显示结果介绍如下&#xff1a; 参考&#xff1a;[https://blog.51cto.com/u_15009374/31…

工厂投产、电池装车,广汽能上动力电池行业的“餐桌”吗?

文 | 智能相对论 作者 | 沈浪 “如果你不在餐桌上&#xff0c;你就会出现在菜单上。”在某种程度上&#xff0c;追逐效益的动力电池行业正在上演着布林肯的“餐桌菜单论”。 于是&#xff0c;我们可以看到&#xff0c;尽管整体的动力电池市场被宁德时代、比亚迪、LG新能源、…

怿星科技Neptune CHT-S测试系统,让智能座舱测试更加高效便捷

随着汽车“智能化”浪潮的推进&#xff0c;汽车的智能化水平正在持续刷新行业认知。在这股智能化潮流中&#xff0c;智能座舱作为客户体验最为直观的部分&#xff0c;其重要性不言而喻。倘若座舱设备出现死机、黑屏、卡顿等现象&#xff0c;都将对客户的使用体验产生非常大的影…
最新文章