简体   繁体   中英

Jar file not using custom font

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM