Results 1 to 3 of 3
  1. #1
    sublixt's Avatar
    sublixt is offline Member
    Join Date
    Jul 2011
    Location
    somewhere
    Posts
    37
    Rep Power
    0

    Default font in jar file not loading

    I have a font, called calg.otf, that i am using in a program. My code works fine when run through eclipse but when i compile it to a jar it stops working;
    Java Code:
    //global variables
    private Font calg;
    private Font de = new Font("Calibre", Font.PLAIN, 22);
    private Font font;
    
    //the call
    public Thing(){
         ...
         calg = grabFont();
    }
    
    //the method
    	public Font grabFont(){	
    		try{
    			URI uri = getClass().getResource("calg.otf").toURI();
    			File file = new File(uri);
    			font = Font.createFont(Font.TRUETYPE_FONT, file);
    			return font.deriveFont(40.0f);
    		}catch(Exception e){
    			g2d.setFont(de);
    			g2d.setColor(Color.GREEN);
    			g2d.drawString("Error! Returning font serif to allow the continue of execution", 30, 30);
    			repaint();
    		}
    		return de.deriveFont(22.0f);
    	}
    how can i fix this to read from the root of a jar file?

  2. #2
    sublixt's Avatar
    sublixt is offline Member
    Join Date
    Jul 2011
    Location
    somewhere
    Posts
    37
    Rep Power
    0

    Default Re: font in jar file not loading

    i figured it out. instead of using file i just used this in my try statement
    Java Code:
    Font font = Font.createFont(Font.TRUETYPE_FONT, getClass().getResourceAsStream("calg.otf"));
    return font.deriveFont(40.0f);
    i think that file cannot read from jar file directories. when i used a url to get the file.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,300
    Rep Power
    20

    Default Re: font in jar file not loading

    Quote Originally Posted by sublixt View Post
    think that file cannot read from jar file directories. when i used a url to get the file.
    An entry in a jar (or a zip) file is not itself a File.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 0
    Last Post: 08-18-2010, 10:31 AM
  2. Replies: 5
    Last Post: 04-25-2009, 08:00 PM
  3. loading a text file
    By nick2price in forum New To Java
    Replies: 2
    Last Post: 12-24-2008, 12:46 AM
  4. Loading of JSP file failed
    By Heather in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 08-06-2007, 01:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •