Ahmad
Ahmad

Reputation: 77

How can I fix FileNotFoundException for embedded fonts (Java)?

I have used some embedded fonts in my project. here is the code used in the main class of the project :

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


    }
}

When I run the project intellij de, it works fine. But after I export the project, the extracted jar file won't run. When I open the jar file using command prompt, it shows this error:

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

Upvotes: 1

Views: 1145

Answers (1)

impune
impune

Reputation: 66

What seems to be causing the exception is loading fonts with File class, designed for accessing files in filesystem, not in .jar archive.
Simple solution would be to use ClassLoader to obtain InputStream, and use InputStream to load the font. Font.loadFont() method accepts any InputStream, so it should work seamlessly. See this question for reference.

The following code should do the job:

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

Upvotes: 1

Related Questions