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