简体   繁体   中英

Libgdx cant load font from jar file

I am trying to initiate a BitmapFont, when I run my project it works but when I jarsplice the project and then run it I get this.

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeExcepti
on: Error reading file: assets\texture\font.fnt (Absolute)
        at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:144)
        at com.badlogic.gdx.graphics.g2d.BitmapFont$BitmapFontData.<init>(Bitmap
Font.java:835)
        at com.badlogic.gdx.graphics.g2d.BitmapFont.<init>(BitmapFont.java:106)
        at catt.main.Game.create(Game.java:105)
        at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplic
ation.java:136)
        at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplicati
on.java:114)
Caused by: java.io.FileNotFoundException: assets\texture\font.fnt (The system ca
nnot find the path specified)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(FileInputStream.java:138)
        at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:140)
        ... 5 more

This is how I load the font.

fontTexture = new Texture("assets/texture/font.tga");
buttonFont = new BitmapFont(new FileHandle("assets/texture/font.fnt"), new TextureRegion(fontTexture), true);
titleFont = new BitmapFont(new FileHandle("assets/texture/font.fnt"), new TextureRegion(fontTexture), true);

They're all all the fields are public static.

LibGDX has already a function to get Filehandle directly from JAR . I load fonts like this:

BitmapFont font = new BitmapFont(Gdx.files.internal("texture/font.fnt"));

You should also make sure you have correctly linked assets folder.

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