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").