Search code examples
javaeclipsefontsfilenotfoundexceptiontruetype

Java RPG Game: Font Loader wont load FontFile .ttf (File Not Found)


i am trying to work at my java rpg game again. First of all, everything is working when i run it in Eclipse via the "play button". When i export the program as an "Runnable Java File", and when i execute it with "java -jar C:[PATH]\Game.jar", it says the FontLoader cant find the FontFile i want to load. What is the proper way to load and return in one Class a TrueTypeFont File?

I pasted some Classes on hastebin.

AssetsClass : Assets.class FontLoader: FontLoader.class

I have an "src" and an "res" folder that are both configured as sourcefolder or "BuildPath" Folders.

In the Res Folder are the Fonts, SpriteSheets and the Levels. The .ttf File is located in /res/fonts/slkscr.ttf. The Class cant find the File. What path do i need to use in order to load the FontFile? Thanks for any Help.

Btw. i am German so some words are may wrong spellen.


Solution

  • Your path string isn't pointing to the file inside your jarfile. I recommend using Class#getResourceAsStream to get the InputStream from inside your jarfile. Make sure the res folder is being packaged into your jarfile and that the path variable represents the absolute path of the file inside your jar,

    Example:

    new FileInputStream(new File(path)))
    

    becomes

    FontLoader.class.getResourceAsStream(absolutePath);