简体   繁体   中英

Using Font from my JAR

I have added a font to my JAR file from eclipse and am trying to use the font within JTextFields. The setText isn't being drawn when I run the application, just a - in the JTextField. I based my code off Exporting font to jar in eclipse .

Below is my code:

//lets load the font
Font font = Font.createFont(Font.TRUETYPE_FONT, Main.class.getClassLoader().getResourceAsStream("Coalition_v2.ttf")); 
font.deriveFont(Font.PLAIN, 14);

txtBrain = new JTextField();
txtBrain.setFont(font);
txtBrain.setText("BRAIN");
txtBrain.setHorizontalAlignment(SwingConstants.CENTER);
txtBrain.setEditable(false);
txtBrain.setColumns(10);
txtBrain.setBounds(521, 727, 90, 40);
contentPane.add(txtBrain);

I know the font is in the correct place as if I change the .getResourceAsStream to /Coalition_v2.ttf then I get the following error from the console:

java.io.IOException: Problem reading font data.
    at java.awt.Font.createFont(Font.java:946)

The Font file is contained within /src/

Output of Jar -tvf as requested:

 0 Sun Mar 31 09:51:56 BST 2013 ngd/
     0 Sun Mar 31 09:51:56 BST 2013 ngd/co/
     0 Sun Mar 31 09:51:56 BST 2013 ngd/co/uk/
     0 Sun Mar 31 09:51:56 BST 2013 ngd/co/uk/TabSync/
   806 Sun Mar 31 08:53:20 BST 2013 ngd/co/uk/TabSync/About$1.class
   556 Sun Mar 31 08:53:20 BST 2013 ngd/co/uk/TabSync/About$2.class
  4762 Sun Mar 31 08:53:20 BST 2013 ngd/co/uk/TabSync/About.class
  1802 Sun Mar 31 08:53:20 BST 2013 ngd/co/uk/TabSync/Help.class
   733 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$1.class
   735 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$10.class
  1117 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$11.class
  1117 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$12.class
  1117 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$13.class
  1117 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$14.class
  1117 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$15.class
  1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$16.class
  1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$17.class
  1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$18.class
  1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$19.class
   733 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$2.class
  1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$20.class
   735 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$21.class
   735 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$22.class
   735 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$23.class
   735 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$24.class
   735 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$25.class
   735 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$26.class
   735 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$27.class
   735 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$28.class
   735 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$29.class
   733 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$3.class
   735 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$30.class
  1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$31.class
  1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$32.class
  1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$33.class
  1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$34.class
  1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$35.class
  1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$36.class
  1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$37.class
  1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$38.class
  1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$39.class
   733 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$4.class
  1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$40.class
  4765 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$41.class
   723 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$42.class
  1185 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$43.class
   697 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$44.class
  1220 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$45.class
   995 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$46.class
   781 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$47.class
   733 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$5.class
   733 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$6.class
   733 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$7.class
   733 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$8.class
   733 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$9.class
  2097 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$Field1Listener.class
 47217 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main.class
  1260 Sun Mar 31 08:53:20 BST 2013 ngd/co/uk/TabSync/FileFiltererpdf.class
  1260 Sun Mar 31 08:53:20 BST 2013 ngd/co/uk/TabSync/FileFiltererjpg.class
991552 Wed Jul 27 19:13:00 BST 2011 ngd/co/uk/TabSync/Coalition_v2.ttf
991552 Wed Jul 27 19:13:00 BST 2011 Coalition_v2.ttf
409717 Thu Mar 21 14:15:32 GMT 2013 nullpdf.pdf
163009 Thu Mar 21 14:29:08 GMT 2013 Javasplash.jpg

The code is being run from Main.class.

EDITED:

Code now looks like this:

   Font font = Font.createFont(Font.TRUETYPE_FONT, this.getClass().getResourceAsStream("/Coalition_v2.ttf")).deriveFont(Font.PLAIN, 14); 

System.out.println(font)  

Sorted. Thanks for your help, and Andrew for your steer in the right direction.

Change

.. Main.class.getClassLoader().getResourceAsStream("Coalition_v2.ttf") .. 

..to something more like..

.. this.getClass().getResource("/Coalition_v2.ttf") // note leading '/' ..

..for an URL then print it out to check it is found.

The leading / means 'look for the root of the class-path' as opposed to from the current package or directory.

There is pretty strange exception handling in createFont method. If FontFormatException or IOException catched, then it just rethrown. In all other cases will be rethrown:

throw new IOException("Problem reading font data.");

So your actually issue is even not the IO problem.

First of all take a look at this point. There is such code in this method:

final File tFile = AccessController.doPrivileged(
    new PrivilegedExceptionAction<File>() {
        public File run() throws IOException {
            return File.createTempFile("+~JF", ".tmp", null);
        }
    }
);

It tries to write into temp directory. You should check if your programm have access to do that.

Solution in case anyone finds this from google was:

  Font font = Font.createFont(Font.TRUETYPE_FONT, this.getClass().getResourceAsStream("/Coalition_v2.ttf")).deriveFont(Font.PLAIN, 14); 

System.out.println(font)  

Andy

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM