Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading sound files from jar file

Tags:

java

jar

audio

So I've been making a game, and have decided that I want to a sound effects for it. I can play sounds just fine until I package the class files and (and the wav audio files) the into the jar, and only to find that it can't find the files. I am using getClass().getResource("sounds/enemyExplode.wav") to get the file. Is there a different method that I should be using? Thanks in advance!

like image 944
DudeOfAwesome Avatar asked Dec 20 '12 23:12

DudeOfAwesome


1 Answers

The class loader's getResource() method operates on resources within the class path. If you have the sounds at the following location in the JAR:

/sounds/enemyExplode.wav

Then you need to use a leading slash in front of the path (just as above) in that call.

Remember a JAR file is really nothing more than a packaged up version of the filesystem (same format as a zip) and the class loader operates upon it as it would if it were a filesystem.

like image 54
dreadwail Avatar answered Nov 15 '22 08:11

dreadwail