0

When I run the jar file it turns into a default font, unlike when I run it in netbeans the code works perfectly fine.

This is my code:

public class login extends javax.swing.JFrame {

    /**
     * Creates new form login
     */
    Font bitWon;
    Font deMono;
    Font deSans;
    Font dotumChe;
    
    public login() {
        initComponents();
        
        try {
            bitWon = Font.createFont(Font.TRUETYPE_FONT, new File("src/8-BIT WONDER.TTF")).deriveFont(36f);
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File("src/8-BIT WONDER.TTF")));
            
            jLabel2.setFont(bitWon);
            
            deMono = Font.createFont(Font.TRUETYPE_FONT, new File("src/determinationmonoweb-webfont.ttf")).deriveFont(24f);
            ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File("src/determinationmonoweb-webfont.ttf")));
            
            jLabel3.setFont(deMono);
            jLabel4.setFont(new Font(deMono.getName(), Font.PLAIN, 18));
            jButton1.setFont(new Font(deMono.getName(), Font.PLAIN, 24));
            
            deSans = Font.createFont(Font.TRUETYPE_FONT, new File("src/determinationsansweb-webfont.ttf")).deriveFont(14f);
            ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File("src/determinationsansweb-webfont.ttf")));
            
            jPasswordField1.setFont(deSans);
            jTextField1.setFont(deSans);
            
            dotumChe = Font.createFont(Font.TRUETYPE_FONT, new File("src/dotumche-pixel.ttf")).deriveFont(10f);
            ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File("src/dotumche-pixel.ttf")));
            
            jLabel7.setFont(dotumChe);
        }
        catch(IOException | FontFormatException e){
            
        }

The fonts (ttf) is placed in the default package, with my main class.

I tried using the inputstream but it only made it not work in netbeans and the jar file too.

  • A .jar file is *one file.* The entries in that one file are not files, they’re part of the .jar file. The correct way to read those fonts is with [getResourceAsStream](https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/lang/Class.html#getResourceAsStream(java.lang.String)). For example, `try (InputStream fontSource = login.class.getResourceAsStream("/8-BIT WONDER.TTF")) { bitWon = Font.createFont(Font.TRUETYPE_FONT, fontSource); }` – VGR May 15 '23 at 12:50
  • Take a look at getResource() : https://stackoverflow.com/questions/2593154/get-a-resource-using-getresource – Joachim Rohde May 15 '23 at 12:52
  • Also, **never** write an empty catch block. In this case, it should contain `throw new RuntimeException(e);`. (Do you really want your program to continue running if the proper fonts couldn’t be loaded?) – VGR May 15 '23 at 13:00
  • Check if .jar file contains font files. If no - read them from system but use proper paths. You can place these files in same folder as .jar file is, then the path will be relative(./somefont.TTF), or use absolute paths. If your .jar file contains font files use getResource() as guys mentioned previously – Eugene Makarenko May 17 '23 at 06:48

0 Answers0