Results 1 to 7 of 7
  1. #1
    endotherm is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default 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:
    Java 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:
    Java 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!

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Can't find resource .wav when in .jar

    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.

  3. #3
    endotherm is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default 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?
    Last edited by endotherm; 03-03-2012 at 07:43 AM.

  4. #4
    endotherm is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default 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:
    Java Code:
    audioInputStream = 
                 AudioSystem.getAudioInputStream(this.getClass().getResourceAsStream(filename));
    the exception is:
    Java 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?
    Last edited by endotherm; 03-03-2012 at 07:59 AM.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.

  6. #6
    endotherm is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Re: Can't find resource .wav when in .jar

    Wonderful, that did it! and I get to drop the extra class to. bonus.

    Java Code:
    Applet.newAudioClip(this.getClass().getResource(_SoundPath)).play();
    Thanks for your help!

    do I need to close the thread? if so, how?
    Last edited by endotherm; 03-03-2012 at 08:31 AM.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.)

Similar Threads

  1. The requested resource () is not available
    By hartmannr76 in forum JavaServer Faces (JSF)
    Replies: 2
    Last Post: 05-31-2012, 07:39 AM
  2. JAVA resource(s)
    By Sparky in forum New To Java
    Replies: 4
    Last Post: 02-15-2011, 01:26 AM
  3. JSP, JSF: Cannot find the resource bundle
    By sunnyjassal in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 02-01-2011, 08:37 PM
  4. The requested resource is not available.
    By ravichandra.t in forum Java Servlet
    Replies: 0
    Last Post: 07-29-2009, 02:18 PM
  5. Velocity, cannot find resource
    By sandor in forum Web Frameworks
    Replies: 1
    Last Post: 05-17-2007, 10:58 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •