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

本文主要是介绍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);}@Overridepublic 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");}}@Overridepublic 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();}@Overridepublic 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 {@FXMLpublic Button alterBtn;@FXMLpublic Button fileBtn;@FXMLpublic TextField fileText;@FXMLpublic AnchorPane rootPane;@FXMLpublic ImageView imgView;@FXMLpublic void initialize() {System.out.println("init");}/*** 文件按钮单击事件*/@FXMLpublic 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("所有类型", "*.*"));//文件显示框 选择的文件返回一个fileFile 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);}/*** 弹出框按钮单击事件*/@FXMLpublic 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

这篇关于GraalVM详细安装及打包springboot、java、javafx使用教程(打包javafx项目篇)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/835995

相关文章

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

SQL Server数据库死锁处理超详细攻略

《SQLServer数据库死锁处理超详细攻略》SQLServer作为主流数据库管理系统,在高并发场景下可能面临死锁问题,影响系统性能和稳定性,这篇文章主要给大家介绍了关于SQLServer数据库死... 目录一、引言二、查询 Sqlserver 中造成死锁的 SPID三、用内置函数查询执行信息1. sp_w

Python UV安装、升级、卸载详细步骤记录

《PythonUV安装、升级、卸载详细步骤记录》:本文主要介绍PythonUV安装、升级、卸载的详细步骤,uv是Astral推出的下一代Python包与项目管理器,主打单一可执行文件、极致性能... 目录安装检查升级设置自动补全卸载UV 命令总结 官方文档详见:https://docs.astral.sh/

Java对异常的认识与异常的处理小结

《Java对异常的认识与异常的处理小结》Java程序在运行时可能出现的错误或非正常情况称为异常,下面给大家介绍Java对异常的认识与异常的处理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参... 目录一、认识异常与异常类型。二、异常的处理三、总结 一、认识异常与异常类型。(1)简单定义-什么是

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(