java - 如何修复嵌入字体 (Java) 的 FileNotFoundException?

标签 java javafx fonts embedded-resource filenotfoundexception

我在我的项目中使用了一些嵌入字体。这是项目主类中使用的代码:

public class Main extends Application {

    public Main() {
    }

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

    @Override
    public void start(Stage stage) throws Exception {

        final Font myEmbeddeFont = Font.loadFont(new FileInputStream(new File("src/main/resources/myEmbeddeFont.ttf")), 14);
        final Font myEmbeddeFont2 = Font.loadFont(new FileInputStream(new File("src/main/resources/myEmbeddeFont2.TTF")), 14);

        Parent root = FXMLLoader.load(getClass().getResource("/main/HomeScreen.fxml"));

        Scene scene = new Scene(root);

        stage = stage;
        stage.setScene(scene);
        stage.setMinWidth(700);
        stage.setMinHeight(600);

        stage.show();


    }
}

当我运行 intellij de 项目时,它工作正常。但是当我导出项目后,提取的jar文件将无法运行。当我使用命令提示符打开 jar 文件时,它显示以下错误:

Exception in Application start method
java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
        at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
        at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$159(LauncherImpl.java:182)
        at java.lang.Thread.run(Thread.java:748)
Caused by: java.io.FileNotFoundException: src\main\resources\myEmbeddeFont.ttf (The system cannot find the path specified)
        at java.io.FileInputStream.open0(Native Method)
        at java.io.FileInputStream.open(FileInputStream.java:195)
        at java.io.FileInputStream.<init>(FileInputStream.java:138)
        at main.Main.start(Main.java:28)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$166(LauncherImpl.java:863)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$179(PlatformImpl.java:326)
        at com.sun.javafx.application.PlatformImpl.lambda$null$177(PlatformImpl.java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$178(PlatformImpl.java:294)
        at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$152(WinApplication.java:177)
        ... 1 more
Exception running application main.Main

最佳答案

导致异常的原因似乎是使用 File 类加载字体,该类设计用于访问文件系统中的文件,而不是 .jar 存档中的文件。
简单的解决方案是使用 ClassLoader获取InputStream,并使用InputStream加载字体。 Font.loadFont() 方法接受任何 InputStream,因此它应该无缝工作。 请参阅this question供引用。

下面的代码应该可以完成这项工作:

final Font myEmbeddeFont = Font.loadFont(getClass().getResourceAsStream("myEmbeddeFont.ttf"), 14);

关于java - 如何修复嵌入字体 (Java) 的 FileNotFoundException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58841951/

相关文章:

java - 在 Spring Boot 中实现简单的异步方法调用时遇到问题

java - JPA实体在服务器重启之前不更新

java - 如何在java中制作实时消息或字符计数器

java - 在maven Java项目上添加JaspersoftStudio生成的font.jar

html - 修复字体差异

java - Netty 编码/解码 Java 对象

Java 与 C JNI - UDP 性能

JavaFX - 最小化舞台的按钮

java - 更新到 gradle 3.3 后构建失败

c++ - 使用 CreateFont() 创建时,Tahoma(MS Shell Dlg 2)字体在 Win7 中看起来被压扁了