Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

java - Accessing custom font from Jar

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. Make sure the case of the font file name & extension is exactly the same in code as on the file system. Windows may not be case sensitive, but Java is.
  2. Check the InputStream returned by getResourceAsStream() for null. If it is null, that indicates the resources was not located.
  3. Put the font in the root of the Jar and add "/" as the prefix to the name.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...