Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何修复嵌入式字体(Java)的FileNotFoundException?_Java_Javafx_Fonts_Embedded Resource_Filenotfoundexception - Fatal编程技术网

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

如何修复嵌入式字体(Java)的FileNotFoundException?,java,javafx,fonts,embedded-resource,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 {

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

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();


    }
}
当我运行IntellijDE项目时,它工作得很好。但在导出项目后,提取的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
应用程序启动方法中出现异常 java.lang.reflect.InvocationTargetException 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处 位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中 位于java.lang.reflect.Method.invoke(Method.java:498) 位于com.sun.javafx.application.LaunchImpl.launchApplicationWithArgs(LaunchImpl.java:389) 位于com.sun.javafx.application.LaunchImpl.launchApplication(LaunchImpl.java:328) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处 位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中 位于java.lang.reflect.Method.invoke(Method.java:498) 位于sun.launcher.launchelper$FXHelper.main(launchelper.java:767) 原因:java.lang.RuntimeException:应用程序启动方法中的异常 位于com.sun.javafx.application.LaunchImpl.launchApplication1(LaunchImpl.java:917) 在com.sun.javafx.application.launchempl.lambda$launchApplication$159(launchempl.java:182) 运行(Thread.java:748) 原因:java.io.FileNotFoundException:src\main\resources\myEmbeddeFont.ttf(系统找不到指定的路径) 位于java.io.FileInputStream.open0(本机方法) 在java.io.FileInputStream.open(FileInputStream.java:195) 位于java.io.FileInputStream。(FileInputStream.java:138) 在main.main.start处(main.java:28) 在com.sun.javafx.application.launchempl.lambda$launchApplication1$166(launchempl.java:863) com.sun.javafx.application.PlatformImpl.lambda$runAndWait$179(PlatformImpl.java:326) 位于com.sun.javafx.application.PlatformImpl.lambda$null$177(PlatformImpl.java:295) 位于java.security.AccessController.doPrivileged(本机方法) 位于com.sun.javafx.application.PlatformImpl.lambda$runLater$178(PlatformImpl.java:294) 位于com.sun.glass.ui.invokelateDispatcher$Future.run(invokelateDispatcher.java:95) 在com.sun.glass.ui.win.WinApplication.\u runLoop(本机方法) 位于com.sun.glass.ui.win.WinApplication.lambda$null$152(WinApplication.java:177) ... 还有一个 运行应用程序main.main时发生异常
导致异常的原因似乎是加载带有File类的字体,该类字体是为访问文件系统中的文件而设计的,而不是.jar存档中的文件。
简单的解决方案是使用获取InputStream,然后使用InputStream加载字体。loadFont()方法接受任何InputStream,因此它应该可以无缝工作。 请参阅以供参考

应使用以下代码执行此操作:

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

由于类似乎是
main
包的一部分,并且路径
main
中似乎没有包含字体的目录,因此最好指定从类路径根开始的完整路径(
/main/resources/myEmbeddeFont.ttf
/resources/myEmbeddeFont.ttf
或可能
/myEmbeddeFont.ttf
)我使用了
最终字体myEmbeddeFont=Font.loadFont(getClass().getResourceAsStream(“resources/ImyEmbeddeFont.ttf”),14);
它工作正常,但该方法区分大小写。谢谢您的帮助。