Can't find resource .wav when in .jar
hello all,
I have a little app that plays a sound (.wav). When I extract the project into a runnable jar, the sound file cannot be found. it works fine in the debugger though (eclipse).
the file is in the .jar at the path /resources/success.wav (I'm on a linux system right now). I attempt to get a handle to the file with:
Code:
File soundFile = new File(this.getClass().getResource('/resources/success.wav').getPath());
but when read from the jar, soundFile.Exists() is false.
I've output the file url to try to diagnose. here it is:
Code:
file:/home/user/EggTimer.jar!/resources/success.wav
the path appears correct, assuming the File class can deal with an archive path.
so any ideas what I can do resolve the path and play the file?
Thanks!
Re: Can't find resource .wav when in .jar
Quote:
the path appears correct, assuming the File class can deal with an archive path.
The entry in the jar file is not, itself, a file so you can't access it using the File class.
You don't say how you are attempting to play the sound, but you have to use the URL instance returned by getResource() and not a File instance created from it. Generally sounds, images and other resources can be accessed using either a file or a url but using a url is more general because the resource can be anywhere including a jar file.
Re: Can't find resource .wav when in .jar
I'm playing the sound with a slightly modified version of this class:
Java play WAV sound file
Edit: ok, after a reread.
so I need to rejigger the class to take a url huh.
shouldn't be too much troublle as long as I can get a stream from it.
woudl GetResourceAsStream(...) be sufficient to create an AudioSystem.AudioInputStream?
Re: Can't find resource .wav when in .jar
ok, i tried to create an AudioInputStream based on the resource stream, and it once again worked in the dubugger, but when run from a jar, I get an exception on that line.
here it is:
Code:
audioInputStream =
AudioSystem.getAudioInputStream(this.getClass().getResourceAsStream(filename));
the exception is:
Code:
user@minty-chan ~ $ java -jar ./EggTimer.jar
java.io.IOException: mark/reset not supported
at java.util.zip.InflaterInputStream.reset(InflaterInputStream.java:286)
at java.io.FilterInputStream.reset(FilterInputStream.java:217)
at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(SoftMidiAudioFileReader.java:135)
at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1111)
at AePlayWave.run(AePlayWave.java:71)
so looks like it can't deal with the compression. lame. any ideas?
Re: Can't find resource .wav when in .jar
You could use Applet.getAudioClip() which, despite the class, is a static method to get an AudioClip from a URL. The clip has a play() method.
Re: Can't find resource .wav when in .jar
Wonderful, that did it! and I get to drop the extra class to. bonus.
Code:
Applet.newAudioClip(this.getClass().getResource(_SoundPath)).play();
Thanks for your help!
do I need to close the thread? if so, how?
Re: Can't find resource .wav when in .jar
You're welcome.
You can mark the thread as "solved" from a link near the top of the page. (I think... I've just had a look and I can't see how. No big deal.)