如何修复嵌入式字体(Java)的FileNotFoundException?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何修复嵌入式字体(Java)的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: srcmain
esourcesmyEmbeddeFont.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以供参考。
以上是关于如何修复嵌入式字体(Java)的FileNotFoundException?的主要内容,如果未能解决你的问题,请参考以下文章
如何修复 @fontface 与默认字体大小 - 如果 @fontface 未加载,布局会中断
如何使用 Gnuplot 4.6 和 AIX 7.2 修复“打开字体 arial 时无法找到/打开字体...”和模糊 png?