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

相关文章

分布式锁在Spring Boot应用中的实现过程

《分布式锁在SpringBoot应用中的实现过程》文章介绍在SpringBoot中通过自定义Lock注解、LockAspect切面和RedisLockUtils工具类实现分布式锁,确保多实例并发操作... 目录Lock注解LockASPect切面RedisLockUtils工具类总结在现代微服务架构中,分布

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

Win10安装Maven与环境变量配置过程

《Win10安装Maven与环境变量配置过程》本文介绍Maven的安装与配置方法,涵盖下载、环境变量设置、本地仓库及镜像配置,指导如何在IDEA中正确配置Maven,适用于Java及其他语言项目的构建... 目录Maven 是什么?一、下载二、安装三、配置环境四、验证测试五、配置本地仓库六、配置国内镜像地址

精选20个好玩又实用的的Python实战项目(有图文代码)

《精选20个好玩又实用的的Python实战项目(有图文代码)》文章介绍了20个实用Python项目,涵盖游戏开发、工具应用、图像处理、机器学习等,使用Tkinter、PIL、OpenCV、Kivy等库... 目录① 猜字游戏② 闹钟③ 骰子模拟器④ 二维码⑤ 语言检测⑥ 加密和解密⑦ URL缩短⑧ 音乐播放

Python pandas库自学超详细教程

《Pythonpandas库自学超详细教程》文章介绍了Pandas库的基本功能、安装方法及核心操作,涵盖数据导入(CSV/Excel等)、数据结构(Series、DataFrame)、数据清洗、转换... 目录一、什么是Pandas库(1)、Pandas 应用(2)、Pandas 功能(3)、数据结构二、安

Spring Boot集成/输出/日志级别控制/持久化开发实践

《SpringBoot集成/输出/日志级别控制/持久化开发实践》SpringBoot默认集成Logback,支持灵活日志级别配置(INFO/DEBUG等),输出包含时间戳、级别、类名等信息,并可通过... 目录一、日志概述1.1、Spring Boot日志简介1.2、日志框架与默认配置1.3、日志的核心作用

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

破茧 JDBC:MyBatis 在 Spring Boot 中的轻量实践指南

《破茧JDBC:MyBatis在SpringBoot中的轻量实践指南》MyBatis是持久层框架,简化JDBC开发,通过接口+XML/注解实现数据访问,动态代理生成实现类,支持增删改查及参数... 目录一、什么是 MyBATis二、 MyBatis 入门2.1、创建项目2.2、配置数据库连接字符串2.3、入

Springboot项目启动失败提示找不到dao类的解决

《Springboot项目启动失败提示找不到dao类的解决》SpringBoot启动失败,因ProductServiceImpl未正确注入ProductDao,原因:Dao未注册为Bean,解决:在启... 目录错误描述原因解决方法总结***************************APPLICA编

深度解析Spring Security 中的 SecurityFilterChain核心功能

《深度解析SpringSecurity中的SecurityFilterChain核心功能》SecurityFilterChain通过组件化配置、类型安全路径匹配、多链协同三大特性,重构了Spri... 目录Spring Security 中的SecurityFilterChain深度解析一、Security