eclipse新建javafx項目并使用jpackage打包成可運行程序

近期要使用javafx做一個電腦上使用的GUI小工具,所以假期剛好在家學(xué)習(xí)了一下javafx,把打包步驟在此記錄一下。

從jdk11起,javafx就已經(jīng)交給社區(qū)維護了,中國官網(wǎng)地址:https://openjfx.cn/

環(huán)境

jpackage打包需要java14以上的jdk版本,所以我下載了jdk17,可以去oracle下載,jdk17開發(fā)者版本下載地址:https://www.oracle.com/cn/java/technologies/downloads/#jdk17-windows
下載之后配置好JAVA_HOME PATH ClASS_PATH等環(huán)境變量,這里省略。

安裝好之后cmd運行 jpackage 看看能不能用,能用說明安裝成功了。

maven要配好國內(nèi)倉庫源,這里也省略。

步驟

1.新建項目
2.打成jar包
3.利用jdk17 jpackage命令行打包成可運行程序

eclipse新建javafx maven項目

eclipse新建File -> New -> Project... -> Maven -> Maven project


img_ab5fd5026934494d8e933685f27e3b08.jpg

下一步 選擇Add Archetypes... groupId: org.openjfx artifactId: javafx-archetype-fxml version:0.0.6


img_294b9ad5bb334960b60645e9aecd2c4d.jpg

點擊ok,Filter填入org.openjfx 選擇javafx-archetype-fxml


img_6d8660a968634eeb9c0d6f0014669765.jpg

下一步 groupId:org.openjfx artifactId:hellofx javafx-version:19 javafx-maven-plugin-version:0.0.8 點擊完成


img_5db05a21b419464f979e50a2ea094a1b.jpg

項目出現(xiàn)之后,eclipse默認jdk11,可以在File -> Properties -> Java Build Path -> Libraries修改jdk的版本,當(dāng)然jdk11也是可以的

修改下pom.xml

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.openjfx</groupId>
    <artifactId>hellofx</artifactId>
    <version>0.0.1</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <maven.compiler.release>11</maven.compiler.release>
        <javafx.version>19</javafx.version>
        <javafx.maven.plugin.version>0.0.8</javafx.maven.plugin.version>
        <javapackager.version>1.6.7</javapackager.version>
    </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>io.github.fvarrui</groupId>
            <artifactId>javapackager</artifactId>
            <version>${javapackager.version}</version>
        </dependency> -->
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <release>${maven.compiler.release}</release>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>${javafx.maven.plugin.version}</version>
                <executions>
                    <execution>
                        <!-- Default configuration for running -->
                        <!-- Usage: mvn clean javafx:run -->
                        <id>default-cli</id>
                        <configuration>
                            <mainClass>org.openjfx.hellofx.App</mainClass>
                            <launcher>app</launcher>
                            <jlinkZipName>app</jlinkZipName>
                            <noManPages>true</noManPages>
                            <stripDebug>true</stripDebug>
                            <noHeaderFiles>true</noHeaderFiles>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            
            <!-- 構(gòu)建jar包plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <archive>
                        <addMavenDescriptor>false</addMavenDescriptor>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>org.openjfx.hellofx.App</mainClass>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>./</Class-Path>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
            <!-- 在打包階段將依賴的jar包導(dǎo)出到lib目錄下 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <type>jar</type>
                            <includeTypes>jar</includeTypes>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

可以看到上面加入了構(gòu)建jar包plugin在打包階段將依賴的jar包導(dǎo)出到lib目錄下 兩個插件

右擊項目Maven->Update Project...一下
修改下module-info.java

module hellofx {
    requires javafx.controls;
    requires javafx.fxml;
    requires transitive javafx.graphics;
    requires javafx.base;

    opens org.openjfx.hellofx to javafx.fxml;
    exports org.openjfx.hellofx;
}

用Run as->Maven Build...執(zhí)行clean javafx:run啟動項目或者直接進入App.java運行主類,可以看到項目正常運行并出現(xiàn)了窗口。


img_fbe05366f53d4bfead8c8edaff997ea2.jpg

打包

Run as->Maven Build...執(zhí)行clean package進行打jar包
打包成功,在target文件夾出現(xiàn)了hellofx-0.0.1.jar和lib文件夾
把hellofx-0.0.1.jar移動到lib文件夾使其在同一個文件夾下
把一個ico圖標(biāo)也復(fù)制到lib文件夾里,作為程序的圖標(biāo)

這里推薦一個png轉(zhuǎn)ico的網(wǎng)站:https://convertico.com/

img_37d3341f2b2f4a83b1f5d4ef41e359b5.jpg

cmd打開lib文件夾 執(zhí)行jpackage打包命令

jpackage --type app-image -n DemoApp -p . -m hellofx/org.openjfx.hellofx.App --vendor zhaohy --app-version 0.0.1 --icon ./TV.ico

稍等片刻后,即可打包成功。在lib下面出現(xiàn)了一個DemoApp的文件夾,進去雙擊DemoApp.exe


img_976f2cfd36f746c2b8d563074ce9da72.jpg

可以看到運行成功。

上面命令里各參數(shù):

--type 或 -t <鍵入字符串>
#要創(chuàng)建的包的類型
#有效值為:{"app-image", "exe", "msi", "rpm", "deb", "pkg", "dmg"}
#如果未指定此選項,則將創(chuàng)建一個與平臺相關(guān)的默認類型。

--name 或 -n <name>
#應(yīng)用程序和/或程序包的名稱。 

-p . 代表當(dāng)前路徑

-m 代表module-info.java里的模塊名和包路徑

--vendor <字符串>
#應(yīng)用程序的供應(yīng)商。 

--app-version <版本>
#應(yīng)用程序和/或軟件包的版本 

--icon 圖片路徑

更多jpackage解釋請看:https://www.bilibili.com/read/cv14523427?spm_id_from=333.999.0.0
上面這位大神從Oracle官方文檔翻譯的,講的比較全。

關(guān)于javafx,貼一個我發(fā)現(xiàn)的寶藏up教程:https://space.bilibili.com/5096022/channel/collectiondetail?sid=210809

關(guān)于非模塊化打包

上面的示例是javafx模塊化的打包方式,很多時候用到的第三方j(luò)ar包不一定支持模塊化,所以記錄一下改成非模塊化之后遇到的這些坑。

從模塊化轉(zhuǎn)成非模塊化非常簡單,只要把根目錄的module-info.java刪除就好了,但是直接刪除之后,它也直接運行不了啟動類了。

所以得新建一個類單獨做啟動,這個類就是一般的啟動類就好,不要繼承Application。
比如新建Launch .java

package org.openjfx.hellofx;

import javafx.application.Application;

public class Launch {
    public static void main(String[] args) {
        Application.launch(App.class, args);
    }
}

直接運行Launch類的main方法就可以啟動javafx項目了。

非模塊化的打包和模塊化的打包命令是有點區(qū)別的,前期步驟都一樣:
1.先運行 clean package命令打出jar包
2.把項目jar包移到target/lib目錄下
3.把圖標(biāo)ico復(fù)制到target/lib目錄下
4.用cmd打開target/lib 此時這里有所有的業(yè)務(wù)jar包和依賴包和圖標(biāo)ico文件
運行以下命令:

jpackage --type app-image -n DemoApp  -i D:\work\workspace2\hellofx\target\lib --main-jar hellofx-0.0.1.jar --main-class org.openjfx.hellofx.Launch --app-version 0.0.1 --icon ./TV.ico --dest D:\work\workspace2\out

-i:指的是需要打包的輸入地址
--main-jar:業(yè)務(wù)jar包名稱
--main-class: 業(yè)務(wù)jar包里的啟動類名稱指定,只有在--main-jar指定之后,這個才有效。
--dest:輸出安裝包的文件夾目錄
上面的文件夾地址換成自己真實的就ok.
--dest這個一定要指定,如果不指定默認會在當(dāng)前目錄陷入打包死循環(huán),不要問我是怎么知道的,不知道是不是jpackage的bug(捂臉),謹記。

如此,就可以打包出非模塊化的可執(zhí)行程序包,親測可以成功運行。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容