0

I've created an executable jar file and added all the .wav, .ttf, .png and .class files into the jar. When I run the jar file it works perfectly when the jar file is contained in the folder where all of the other files needed are.

However I am trying to include all the files needed to execute my application properly inside the jar file. I used the command:

jar cvfe App.jar Main *.class *.png *.ttf *.wav

on windows command prompt. When executing the jar file outside the folder which contains all the necessary files listed above, only the image files and class files seem to be included in the jar file, the font files and sound files do not work and the GUI shows a default font and sound does not work either. The stack trace on command prompt prints that all the files needed are being added when i execute the above command but it doesn't seem to work.

*****EDIT******

Heres how I am loading the font.

 Font font = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream("C&LBOLD.ttf")).deriveFont(50f);

Heres how I am loading the .wav files:

File waveFile = new File("sounds//berlin.wav");
fooll
  • 39
  • 8

2 Answers2

4

Most likely you are not loading those files through the class loader.

If you provide the code where you load those files we can check for sure.

[Edit] Thanks for posting the extra code - It's as suspected. You need to load the files using a class loader.

So, for example, where you have...

new FileInputStream("C&LBOLD.ttf")

You should have something like this instead...

this.getClass().getClassLoader().getResourceAsStream("C&LBOLD.ttf")

Java will then know to look on the classpath (and hence in the jar) for the resource.

Phil Anderson
  • 3,146
  • 13
  • 24
0

You should use

Font font = Font.createFont(Font.TRUETYPE_FONT, this.getClass().getClassLoader().getResourceAsStream("C&LBOLD.ttf")).deriveFont(50f);

this is how you load resources located in the classpath (whether from a .jar or a directory). Just keep in mind this may not work in some containers if you are deploying the jar as part of a Web application, for instance.

It's also a good idea to check the input for null to make sure the resource exits.

vempo
  • 3,093
  • 1
  • 14
  • 16