来自 jlink 的 jar 图像不显示 org.kordamp.materialdesign2 图标,但它们在 intellij 中运行程序时加载

问题描述 投票:0回答:1

javafx 应用程序不显示 org.kordamp.materialdesign2 图标

我最近使用 javafx 18 完成了一个学校管理程序,该程序使用正常的 intellij 运行配置正常运行,但无法从 org.kordamp.materialdesign2 加载所有图标。我扫描了整个网络并找到了一些有关服务文件的答案。我的模块信息文件中包含所有三个必需的包 org.kormap.ikoli.core、org.kordamp.javafx 和 org.kordamp.materialdesign2

当 jar 运行时,它会显示这样的消息

Exception in thread "JavaFX Application Thread" java.lang.UnsupportedOperationException: Cannot resolve 'mdi2o-office-building' at org.kordamp.ikonli.AbstractIkonResolver.resolve(AbstractIkonResolver.java:61) at org.kordamp.ikonli.javafx.IkonResolver.resolve(IkonResolver.java:73) at org.kordamp.ikonli.javafx.FontIcon.lambda$new$2(FontIcon.java:76) at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:360) at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80) at javafx.beans.property.ObjectPropertyBase.fireValueChangedEvent(ObjectPropertyBase.java:106) at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:113) at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:147) at javafx.css.StyleableObjectProperty.set(StyleableObjectProperty.java:82) at org.kordamp.ikonli.javafx.FontIcon.setIconCode(FontIcon.java:231) at org.kordamp.ikonli.javafx.FontIcon.<init>(FontIcon.java:97) at manager.school_manager.EntryController.initialize(EntryController.java:115) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2573) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435) at manager.school_manager.Entry.start(Entry.java:56) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846) at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455) at com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428) at java.base/java.security.AccessController.doPrivileged(AccessController.java:399) at com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427) at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96) at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174) at java.base/java.lang.Thread.run(Thread.java:1589)

error messages on running jar Program without icons on running jar Program showing icons when run in intellij

module-info.java with all three co-dependencies

尝试使用其文档中所述的shadow jar插件

build.gradle file showing use of shadow jar plugin

还用 winrar 打开 jar 文件并检查 Manifest/Manifest.MF/services,这是我发现的

it shows two files related to ikonli. Am not quit versed to services in general so i cant tell if these would surfice.*** I would gladly accept any help that would help me resolve this problem***

java javafx javafxports javafx-webengine
1个回答
0
投票

示例应用程序

  • 使用 jlink 将应用程序链接到为部署而压缩的映像中,而不是将应用程序打包为可执行 jar(因为这是提问者在评论中表示他们想要做的事情)。
  • 使用maven进行构建和打包,因为这是我熟悉的。
  • 将创建一个只能在与用于构建应用程序的机器相同的平台类型和架构上使用的应用程序。

构建需要安装 Maven 和 JDK 20。部署是独立的,除了运行的操作系统之外没有任何依赖项。

src/main/java/module-info.java

module com.example.materialicons {
    requires javafx.controls;
    requires org.kordamp.ikonli.javafx;
    requires org.kordamp.ikonli.materialdesign2;

    exports com.example.materialicons;
}

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>materialicons</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>materialicons</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>20.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.kordamp.ikonli</groupId>
            <artifactId>ikonli-javafx</artifactId>
            <version>12.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.kordamp.ikonli</groupId>
            <artifactId>ikonli-materialdesign2-pack</artifactId>
            <version>12.3.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.11.0</version>
                <configuration>
                    <source>20</source>
                    <target>20</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.8</version>
                <executions>
                    <execution>
                        <id>default-cli</id>
                        <configuration>
                            <mainClass>com.example.materialicons/com.example.materialicons.IconApplication</mainClass>
                            <launcher>iconapp</launcher>
                            <jlinkZipName>iconapp</jlinkZipName>
                            <jlinkImageName>iconapp</jlinkImageName>
                            <noManPages>true</noManPages>
                            <stripDebug>true</stripDebug>
                            <noHeaderFiles>true</noHeaderFiles>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

src/main/java/com/example/materialicons/IconApplication.java

Material Design 有图标包,名称为 A -> Z。这里我们只使用 A 图标包,其他图标包的使用方法类似。

package com.example.materialicons;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
import org.kordamp.ikonli.javafx.FontIcon;
import org.kordamp.ikonli.materialdesign2.MaterialDesignA;

public class IconApplication extends Application {
    @Override
    public void start(Stage stage) {
        FlowPane layout = new FlowPane();
        layout.setPadding(new Insets(10));
        layout.setPrefWrapLength(500);
        for (MaterialDesignA iconEnumVal : MaterialDesignA.values()) {
            FontIcon icon = new FontIcon(
                    iconEnumVal.getDescription()
            );
            icon.setIconSize(40);
            layout.getChildren().add(
                    icon
            );
        }

        Scene scene = new Scene(new ScrollPane(layout), 530, 400);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}

构建

构建命令(从项目根目录执行):

mvn clean javafx:jlink

构建输出:

[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------< com.example:materialicons >----------------------
[INFO] Building materialicons 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ materialicons ---
[INFO] Deleting /Users/username/dev/materialicons/target
[INFO] 
[INFO] >>> javafx-maven-plugin:0.0.8:jlink (default-cli) > process-classes @ materialicons >>>
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ materialicons ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/username/dev/materialicons/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.11.0:compile (default-compile) @ materialicons ---
[INFO] Changes detected - recompiling the module! :source
[INFO] Compiling 2 source files with javac [debug target 20 module-path] to target/classes
[INFO] 
[INFO] <<< javafx-maven-plugin:0.0.8:jlink (default-cli) < process-classes @ materialicons <<<
[INFO] 
[INFO] 
[INFO] --- javafx-maven-plugin:0.0.8:jlink (default-cli) @ materialicons ---
[INFO] Building zip: /Users/username/dev/materialicons/target/iconapp.zip
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

使用示例

从项目根目录执行。调整适合您的操作系统类型的命令语法。

mkdir tmp
cd tmp
unzip ../target/iconapp.zip 
bin/iconapp

输出示例

© www.soinside.com 2019 - 2024. All rights reserved.