可执行jar
参考:https://openjfx.io/openjfx-docs/
javaFx 可执行jar (jdk 11)
目录 src/main/java/com.mypack
AppMain.java
package com.mypack; import javafx.application.Application; /** * 打包运行 * 错误: 缺少 JavaFX 运行时组件, 需要使用该组件来运行此应用程序。 * 原因:模块化 Java 程序与非模块化 Java 程序的启动方式有所不同 * 解决:单独创建一个启动类 */ public class AppMain { public static void main(String[] args) { Application.launch(Main.class, args); } }
Main.java
package com.mypack; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.StackPane; import javafx.stage.Stage; import org.apache.commons.lang3.StringUtils; public class Main extends Application { @Override public void start(Stage stage){ String str = "Say 'Hello World'"; StringUtils.isNotBlank(str); Button btn = new Button(); btn.setText("Say 'Hello World'"); btn.setOnAction(actionEvent -> System.out.println("Hello World!")); StackPane root = new StackPane(); root.getChildren().add(btn); Scene scene = new Scene(root, 300, 250); stage.setTitle("Hello World"); stage.setScene(scene); stage.show(); } }
目录 src/main/java/
module-info.java
module com.mypack {
requires javafx.controls;
requires javafx.fxml;
requires org.apache.commons.lang3;
opens com.mypack to javafx.graphics, javafx.fxml, org.apache.commons.lang3;
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>mypack</groupId> <artifactId>mypack</artifactId> <version>1.0-SNAPSHOT</version> <properties> <javafx.version>11</javafx.version> </properties> <dependencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.8.1</version> </dependency> <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> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <release>11</release> <source>11</source> <target>11</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.1.1</version> <configuration> <archive> <manifest> <!--入口函数--> <mainClass>com.mypack.AppMain</mainClass> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> </manifest> </archive> </configuration> </plugin> <!--将依赖拷贝到lib目录下--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.1.1</version> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
cmd运行
java -jar ****************\mypack-1.0-SNAPSHOT.jar
可执行jar -> jmode -> 可执行应用程序
jmod
- C:\Users\*\Desktop\a\jmods
- C:\Users\*\Desktop\a\lib\javaMoudle-1.0-SNAPSHOT.jar
jmod create --class-path lib\javaMoudle-1.0-SNAPSHOT.jar jmods\javaMoudle-1.0-SNAPSHOT.jmod
jlink
jlink --module-path "模块目录" --add-modules "你的模块名" --launcher 模块名=模块名/主方法所在类路径 --output "输出目录" --strip-debug --compress 2
jlink --module-path jmods --add-modules javaMoudle --launcher javaMoudle=javaMoudle/service.Main --output out
//如果可执行程序引用到第三方jar,只需要将jmod和第三方jar放到模块路径中,不需要添加模块名可直接连接;
//modulepath:These modules can be modular JAR files, JMOD files, or exploded modules
jlink --module-path jmods --add-modules javaMoudle --launcher javaMoudle=javaMoudle/service.Main --output out