3

I have the following code in a static method:

clips.open(AudioSystem.getAudioInputStream(Sound.class.getResourceAsStream("folder/sound.wav")));

Also, folder is in the same directory as Sound.java. When I run the program in Eclipse, the sound is played. However, when I export the file to a JAR file, the sound no longer plays.

If I change getResourceAsStream() to getResource(), both Eclipse and the JAR file play the sound. Why does this happen? I have read around and many people suggest that getResourceAsStream() simply doesn't work in JAR files. Is this the case, and if so, why not?

null
  • 2,060
  • 3
  • 23
  • 42
  • 1
    I doubt your `getResource` works any differently in the JAR case. – Sotirios Delimanolis Jun 07 '15 at 15:06
  • Nope. With getResource(), the JAR file plays the sound properly. Others also seem to observe this, such as [here](http://stackoverflow.com/a/15311535) and [here](http://stackoverflow.com/a/16048907). – null Jun 07 '15 at 15:54
  • Interesting links. I notice that on those threads they don't follow through with establishing the true reason why one works but the other doesn't. I suspect it involves the use of InputStream or not (as per my answer). But it also makes sense to declare that life is too short and if you have something that works, move on! – Phil Freihofner Jun 08 '15 at 17:46

1 Answers1

0

I suspect that the issue is that Class.getResourceAsStream(String) returns an InputStream, and Class.getResource(String) returns a URL. Thus, in the first case, we are in effect using

AudioSystem.getAudioInputStream(InputStream inputStream) 

and in the second case, using

AudioSystem.getAudioInputStream(URL url).

Why does this matter? I think the main issue is that when the InputStream is the parameter, mark/reset tests are done on your audio file and it is failing. When the URL is used as a parameter, this test does not occur. You might check out the API for AudioSystem.getAudioInputStream to confirm what I am reporting here.

Do you get an error message when the getResourceAsStream fails? Does it mention the mark/reset test error by chance? If you are getting a different error message then this might not be the correct answer and something else is going on.

Phil Freihofner
  • 7,645
  • 1
  • 20
  • 41
  • No mark/reset error. However, when printing the stack trace, there are some `FileNotFoundException`s. However, this is expected. The way my program works is that the user selects a folder containing files, and these files are loaded into the program. Therefore, there may be times when the user does not have a certain file (which is not required), and this should not be a problem. Also, I get `FileNotFoundException` for both `getResource()` and `getResourceAsStream()`, so this should not be the reason the sounds don't play with `getResourceAsStream()`. – null Jun 08 '15 at 08:07
  • 1) Can you isolate an error message for the failing instance? In other words, try to load a file that you know exists and should not elicit a "not found" and have the error occur when you use getResourceAsStream but not GetResource? 2) I once had a situation where the InputStream stage failed (due to mark/reset) but the calling method, getAudioInputStream() reacted to this fail by reported it as a "file not found" rather than passing up the lower level error. The information you just gave me does not fully rule out this possibility, though the true reason remains a mystery. – Phil Freihofner Jun 08 '15 at 17:41