0

Inside NetBeans my custom font loads properly from this set of code but fails to load when I run my program from the executable jar file

code

    public static void main(String[] args) {
    Arcanus arc = new Arcanus();  
    try {
        Font customFont = Font.createFont(Font.TRUETYPE_FONT, new File("Golden-Sun.ttf")).deriveFont(12f);
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File("Golden-Sun.ttf")));
        arc.setFont(customFont);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (FontFormatException e) {
        e.printStackTrace();
    }
}

any help would be apreciated

taringamberini
  • 2,719
  • 21
  • 29
Mark9135
  • 101
  • 11

1 Answers1

2

Embedded resources should NOT be read from a File object. The File object is used for reading files in the local file system. Once your file is jarred, it becomes a resource and should be read as such. You can read it as a InputStream by using getClass().getResourceAsStream(). For example

InputStream is = getClass().getResourceAsStream("/Golden-sun.tff");
Font font = Font.createFont(Font.TRUETYPE_FONT, is);

Where Golden-sun.tff is on the class path (direct child of src for development)

Root
   src
      Golden-sun.tff
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720