title: Java - How to Load a Custom Font From a Resources Folder
body: <p>I'm using java swing to create an app, but I'm also stuck trying to load some font!
  Here is the problem:
  I Have a resource folder with some custom font, when I try to load them using this code:</p>
  <pre><code>    public static Font CustomFont(String path) {
          Font customFont = loadFont(path, 24f);
          System.out.println(customFont == null);
          GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
          ge.registerFont(customFont);
          return customFont;
  
      }
      public static Font loadFont(String path, float size){
          try {
              Font myFont = Font.createFont(Font.TRUETYPE_FONT, Launcher.class.getResourceAsStream(path));
              return myFont.deriveFont(Font.PLAIN, size);
          } catch (FontFormatException | IOException e) {
              e.printStackTrace();
              System.exit(1);
          }
          return null;
      }
  </code></pre>
  <p><strong>UiFonts.java</strong></p>
  <pre><code>    public static Font Nunito;
  
      public static void init() {
          Nunito = CustomFont(&quot;Fonts/Nunito/Nunito-BlackItalic.ttf&quot;);
      }
  </code></pre>
  <p>This is my resource folder:
  <a href="https://i.stack.imgur.com/3TfpW.png" rel="nofollow noreferrer">My project file and folder</a></p>
  <p>It always show me error depending on the path.</p>
  <p>Example: Cant read font file data.</p>
  <h1>Solution</h1>
  <p>I needed to add <code>/</code> before the <code>Font</code> like this:</p>
  <p><code>Nunito = CustomFont(&quot;/Fonts/Nunito/Nunito-BlackItalic.ttf&quot;);</code></p>
  <p>Also my resources folder wasn't in my java folder. Now it look like this:
  <a href="https://i.stack.imgur.com/f2nw6.png" rel="nofollow noreferrer">Solution project file and code</a></p>