Using a custom font - inside JAR file
This code works perfectly fine when I run it as a class file (the font is located in the same directory) - however when I package it into a JAR file it can't find the font and the program crashes.
The font is definitely being included in the JAR archive, so I suspect I'm not using the correct syntax to point to it.
How would I modify this so that it works in both cases?
// Text area font
try
{
// Determine the font file path
String path = System.getProperty("java.class.path");
String fileseparator = System.getProperty("file.separator");
int pathlength = path.length();
if (path.charAt(pathlength-1) != fileseparator.charAt(0)) { path += fileseparator; }
path += "Inconsolata.pfa";
// Make the font the proper size
Font STEFont = Font.createFont(Font.TYPE1_FONT, new File(path));
STEFont = STEFont.deriveFont(15.0f);
// Add the font to STE
jTextArea.setFont(STEFont);
}
catch (Exception e)
{
e.printStackTrace();
}
When running the JAR file using Windows Explorer there are no error messages (literally,nothing seems to happen). When I run it from the console this is returned:
java.io.IOException: Can't read STE.jar\Inconsolata.pfa
at java.awt.Font.createFont(Font.java:978)
at TextEditorFrame.<init>(TextEditorFrame.java:605)
at TextEditor.<init>(TextEditor.java:26)
at TextEditor.main(TextEditor.java:104)
An unexpected problem occurred. Please report this so it can be fixed, including the error message that you see below.
java.lang.NullPointerException
The last part is a custom error message I've put into a catch block in the TextEditor class (the class with the main method).
TextEditorFrame is a class that extends JFrame, and the code I posted above in the first code block is located inside the constructor of this class.Update: If you want to attempt to reproduce this, you'll probably need the font as well. The font can be downloaded here: http://www.levien.com/type/myfonts/inconsolata.html (use the link titled "pfa file").
leiger
Junior Poster in Training
91 posts since Jun 2010
Reputation Points: 33
Solved Threads: 6
Does an overload of createFont() method take a URL instead of a File?
The classpath for code in a jar is the jarfile which is NOT useable in a File constructor.
Add some debugging code to print out the values returned by the various method calls you are using.
To get a file from the jar file, look at using the getResource method.
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
I've been using the getResource method for gif files just fine, but that didn't seem to work for the font. I think the reason was what you mentioned - that there is no overload of createFont() that takes a URL.
I'll have another look at the getResource method then, perhaps I missed something last time I looked. I'll check again to make sure there's no way for me to pass a URL to createFont().
leiger
Junior Poster in Training
91 posts since Jun 2010
Reputation Points: 33
Solved Threads: 6
Okay, the only two versions of the createFont() method are:
static Font createFont(int fontFormat, File fontFile)
static Font createFont(int fontFormat, InputStream fontStream)
The only versions of the getResource() method are:
URL getResource(String name)
InputStream getResourceAsStream(String name)
So... if I understand this correctly I can use the second version of each. The getResource one to get an InputStream for the font... and then plug that into the second createFont method?
I'll see if I can get that working now.
leiger
Junior Poster in Training
91 posts since Jun 2010
Reputation Points: 33
Solved Threads: 6
Works perfectly! Thanks! :)
Posting a list of parameters and return types helped me to find the answer. It was right in front of me the whole time - I just didn't notice.
leiger
Junior Poster in Training
91 posts since Jun 2010
Reputation Points: 33
Solved Threads: 6
Congratulations. There was a connection between the URL and the InputStream needed int the method call.
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656