This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java getResourceAsStream not working in JAR

 
Michael Getz
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 20713
29
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, line 23 that it is referring to in that stack trace is this line:

 
Michael Getz
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 20713
29
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting! Thanks for the feedback, Michael!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic