aspose file tools*
The moose likes Java in General and the fly likes Java getResourceAsStream not working in JAR Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java getResourceAsStream not working in JAR" Watch "Java getResourceAsStream not working in JAR" New topic
Author

Java getResourceAsStream not working in JAR

Michael Getz
Greenhorn

Joined: Oct 22, 2012
Posts: 7
Ok so I have been working on this java project in Eclipse. When I run the program in eclipse the sound file that I am trying to load via the getResourceAsStream method loads just fine. Although, after I export to a JAR the program locks up when I get to the part that it loads the sound.

Here is the code:



Before anyone asks, I have checked the JAR to see if the resource made it there when Eclipse exported it, and yes it is there in the sound folder. I have found out though that with the above code if I put a folder called "sound" in the same directory as the JAR and put the wave file in it, it will load it fine. So my question then is: Why doesn't it load the file from within the JAR instead?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18883
    
    8

Hi Michael, welcome to the Ranch!

You say that the program "locks up" when you run it from the JAR. And you say that's because the program can't find the sound file. But I don't think that can be right, because if the program couldn't find the sound file that wouldn't lock it up, it would just catch and log the exception (lines 21-24 of your posted code) and carry on. I think something else must be the problem.
Michael Getz
Greenhorn

Joined: Oct 22, 2012
Posts: 7
Well, no it doesn't lock up entirely but my keylistener stops working because there was a call to the the sound clip in the key listener. Other threads do continue to run though.
Michael Getz
Greenhorn

Joined: Oct 22, 2012
Posts: 7
The stack trace is:

java.io.IOException: mark/reset not supported
at java.util.zip.InflaterInputStream.reset(Unknown Source)
at Java.io.FilterInputStream.reset(Unknown Source)
at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(Unknown Source)
at Javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at com.shootemup.Sound.<init>(Sound.java:23)
at com.shootemup.ShootemUpApp$1.run(ShootemUpApp.java:10)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
Michael Getz
Greenhorn

Joined: Oct 22, 2012
Posts: 7
Also, line 23 that it is referring to in that stack trace is this line:

Michael Getz
Greenhorn

Joined: Oct 22, 2012
Posts: 7
Wow, after working on this all night last night and more this evening I figured this out. Sorry, that no one got to me in time.

Turns out that this is a known problem in java se 7.

The following quote is from: http://stackoverflow.com/questions/9322651/java-io-ioexception-mark-reset-not-supported-for-static (I hope posting this link doesn't break any sort of rules)

I had to deal with a very similar problem, and posted it here:

mark/reset exception during getAudioInputStream()

This form: .getResourceAsStream(fileName) returns an InputStream which throws a mark/reset exception if the file is not markable. The explanation I got is that there used to be a default "first guess" of .wav, but this is no longer the first guess (as of Java 7). There is a better, fuller description at Oracle's bug database for #7095006.

Use this form and you should be okay, because it doesn't require the intermediate step (InputStream) that needs to support marking & resetting:

URL url = AudioMixer.class.getResource(fileName);
AudioInputStream ais = AudioSystem.getAudioInputStream(url);


Here is the code that works now if anyone gets here later and is interested:

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18883
    
    8

Interesting! Thanks for the feedback, Michael!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java getResourceAsStream not working in JAR