I am going to use custom font in my application. For that I am using Font.createFont()
method. My code is given below. It works fine when I run my main class using command
java myAppl.class
The font file is in same directory that of my class file. But when I bundled all files and font files in JAR and then run my application from JAR, the custom font do not loaded. Why?
InputStream is = this.getClass().getResourceAsStream("myfont.TTF");
uniFont=Font.createFont(Font.TRUETYPE_FONT,is);
Font f = uniFont.deriveFont(24f);
What should I do?
InputStream
returned by getResourceAsStream()
for null
. If it is null
, that indicates the resources was not located. As Andrew response, I've tested exactly code above and works:
InputStream is = this.getClass().getResourceAsStream("/myfont.TTF");
uniFont=Font.createFont(Font.TRUETYPE_FONT,is);
Just missing "/" before file name. Note: jar package by Netbeans
Try copying the font to the jre/lib/font folder
or use the package-qualified name ("com.mypackagename.myfont.TTF")
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.