I'm trying to set a font "bilboregular.ttf" found in the resources folder under the root of the jar file. If i run from netbeans the font is set successfully, however if from the jar file it isn't
CODE:
main method:{
String s = System.getProperty("user.dir") + "\\src\\resources\\" + "bilboregular.ttf";
File a = new File(s);
if (a.exists()) {
this.loadFont(s);
} else {
this.loadFont(this.getClass().getResource("/resources/" + "bilboregular.ttf").getFile());
}
}
public void loadFont(String s) {
try {
font = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, new File(s));
font = font.deriveFont(Font.PLAIN, 20);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(font);
setFonts();
} catch (Exception ex) {
}
}
public boolean setFonts() {
jLabel3.setFont(font);
jLabel4.setFont(font);
return true;
}
Your main can't use getClass()
because its static
(assuming you mean public static void main(String args[])
with "main").
Try this code:
InputStream is = getClass().getResourceAsStream("/resources/bilboregular.ttf");
Font f = Font.createFont(Font.TRUETYPE_FONT, is);
if this doesn't work: please post the structure of your JAR and and the thrown exception (eg. use a Logger in your catch
Block).
Btw. an empty exceptionblock is not a good idea here since IO operations can fail in many cases.