0

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.

Gandalf1783
  • 11
  • 1
  • 8
  • `getResourceAsStream("/fonts/slkscr.ttf")` (case sensitive, forward slashes and check the jar (zip format)). Also [register the font for swing](https://stackoverflow.com/questions/5652344/how-can-i-use-a-custom-font-in-java) may be needed. – Joop Eggen Apr 18 '19 at 13:50

1 Answers1

0

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);
Benjamin Urquhart
  • 1,626
  • 12
  • 18
  • As soon as i replace my FileInputStream with getResourceAsStream i get this error: "java.io.IOException: Problem reading font data.". So the error occures in line 14, the line with the return statement. – Gandalf1783 Apr 18 '19 at 13:49
  • 1
    All in all, i made it to fix the problem. Thank you! – Gandalf1783 Apr 18 '19 at 13:55