I wrote a program that uses wav files. It works in Eclipse but when I create an executable jar file the sounds no longer plays.
Any ideas as to what's going on and the fix that is required?
Thanks
Curt
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
I wrote a program that uses wav files. It works in Eclipse but when I create an executable jar file the sounds no longer plays.
Any ideas as to what's going on and the fix that is required?
Thanks
Curt
Are there any error messages?
Open a console window, change to the folder with the jar file and enter the command:
java -jar YOURJARFILENAMEHERE.jar
Copy and paste the full contents of the console screen here.
Do all your catch blocks call the printStackTrace() method?
How are you reading the files? If the .wav files are in the jar file you must access them as resources and not as files. Use the getResourceAsStream() method to read "files" in a jar file.
Norm,
Thanks for the reply. There were no errors when I ran java -jar ... It is probably in that I'm reading the wav files as "files" and not using getResourceAsStream. Now I just have to figure out how get that to work with the code I have written.
Thanks
PS...hope you faired well with all the storms. I'll be in Central MO next week. Can't wait to get back to my old stomping grounds.
Do all your catch blocks call the printStackTrace() method?
We're 90 miles from Joplin.
Haven't had any tornadoes here (yet).
Central MO - Rolla?
I placed printStackTrace in all the catch blocks but nothing is being thrown.
Here is the code after I briefly tried to getResourceAsStream working. It was a quick attempt before I got called away.
public class PlaySounds { private InputStream fileName = null; /* public PlaySounds (String fName) { fileName = fName; //Thread playFileThread = new Thread (playFileRunnable); playFile(); } */ public PlaySounds (InputStream fName) { fileName = fName; } public void playFile() { Runnable playFileRunnable = new Runnable() { int EXTERNAL_BUFFER_SIZE = 524288; // File soundFile = new File(fileName); public void run() { AudioInputStream audioInputStream = null; try { //audioInputStream = AudioSystem.getAudioInputStream(soundFile); audioInputStream = (AudioInputStream) fileName; } catch (Exception e) { e.printStackTrace(); } AudioFormat format = audioInputStream.getFormat(); SourceDataLine auLine = null; DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); try { auLine = (SourceDataLine) AudioSystem.getLine(info); auLine.open(format); } catch (Exception e) { e.printStackTrace(); } auLine.start(); int nBytesRead = 0; byte[] abData = new byte[EXTERNAL_BUFFER_SIZE]; try { while (nBytesRead != -1) { nBytesRead = audioInputStream.read(abData, 0, abData.length); if (nBytesRead >= 0) { auLine.write(abData, 0, nBytesRead); } } } catch (Exception e) { e.printStackTrace(); } finally { auLine.drain(); auLine.close(); } } // public void run() }; // Runnable playFileRunnable = new Runnable() Thread playFileThread = new Thread(playFileRunnable); playFileThread.start(); } // private void playFile()
Here's the calling lines:
InputStream boxingBellIn = getClass().getClassLoader().getResourceAsStream("boxing_bell.wav"); PlaySounds playSound = new PlaySounds(boxingBellIn);
I know this line "audioInputStream = (AudioInputStream) fileName;" is incorrect.
----
I'm from the Lake of the Ozarks area. I graduated from Rolla in 1998 with a BS in Aerospace Engineering when it was UM-Rolla and not MUST (such a goofy abbreviation)
Sorry, I don't know anything about using AudioStreams.
If the code worked in the IDE, then the only changes should be to a constructor to use an InputStream vs a File or something like that.
UMR 1972
No problem. Thanks for the help.
Curt
Okay, I got it working. I found the code at anyexamples.com that reads the file as a stream. It can be found here.
So I created an executable jar that runs and the sound works....on the computer I wrote the code. When I transferred it to a different computer, it doesn't work.
If anyone can help I'd appreciate it. I'm off to search the web to try and figure out this new problem.
Thanks
Curt
How did you test it on your computer? Did you move the jar file to an empty test folder?
To see if there are any errors when running a jar file, open a command prompt window, change directory to the folder holding the jar file and enter:
java -jar YOURJARFILENAMEHERE.jar
Copy the console window and paste here:
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'
Paste here.
I have a stupid question: are you sure the wav file is in your Jar? I know eclipse has a really awesome frakking annoying tendency to not include all the files you think it should include in the Jar. Try viewing the contents of the Jar from the command line to see whether it's in there: Viewing the Contents of a JAR File (The Java™ Tutorials > Deployment > Packaging Programs in JAR Files)
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
I moved it to a completely different computer.
Here is the error I get:
javax.sound.sampled.UnsupportedAudioFileException: could not get audio input str
eam from input file
at javax.sound.sampled.AudioSystem.getAudioInputStrea m(Unknown Source)
at AePlayWave.run(AePlayWave.java:53)
Coincidently, I was getting this error in the IDE but I eventually got it to compile and run without errors. The way I call the wav file is exactly how it is shown in the code from the link in my previous reply (anyexamples.com), which looks like this:
new AePlayWave("boxing_bell.wav").start();
Kevin - not a stupid question. That was the first thing that I checked and the wav files were in the jar file.
Thanks for the replies and I appreciate the help on this.
Curt
Another stupid question: what happens if you move your Jar to a completely different folder (on the computer that it works)? Does it still work? The reason I ask is, it might be picking up an external file instead of the file inside the Jar.
For what it's worth, I have a Jar that plays a wav that you could check out if you wanted to. I put the wav file in the same folder as the class files (again, I'm really confused by how Eclipse decides which files to include in Jars, but that's a different rant), and the code is as follows:
private Clip music; //... private void startSong(){ try{ AudioInputStream stream = AudioSystem.getAudioInputStream(getClass().getResource("OverAndDone.wav")); music = AudioSystem.getClip(); music.open(stream); music.start(); } catch(Exception e){ e.printStackTrace(); music = null; } }
You can download the Jar (or view it in applet form, or look at the code) here: Little Bird, Fly Away
That site may or may not work in Internet Explorer :p
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
You can NOT use the File class. You need to use getResourceAsStream() to read 'files' that are in a jar file.
See the end of post #2
KevinWorkman (June 7th, 2011)
Okay..I went to the original computer and moved the jar file into a folder by itself and it didn't work. I then added the wav files to the folder and it worked.
So, then back to the "new" computer I copied the same wav files to the folder with the jar file and it still didn't work. It was until I moved the wav files from the original computer to the new computer did it work. This makes no sense as the wav files on the original computer came from the "new" computer.
So I double checked that the wav files were in the jar and they are there. Even though they are in the jar file do I still have to distribute them as separate files with the jar file? I'm having trouble with the nuances of Java...it just doesn't seem very intuitive.
Thanks
Curt
I think it's pretty intuitive (if it wasn't, I wouldn't have been able to guess that moving the Jar would cause it to break), once you understand the difference between something in a Jar and something next to a Jar. As Norm pointed out (twice now), you can't use File to access stuff inside the Jar- use getResource() or getResourceAsStream(), as I did in my example.
If you're using File, you aren't using what's in the Jar.
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
cl2606 (June 7th, 2011)
You can put everything in the jar file. When reading "files" from the jar file, you can NOT use any file classes. You must treat the "files" as resources.
How are you reading the wav files? The examples you've posted use file classes. They will look outside of the jar file.
Whoops, duplicate effort.
cl2606 (June 7th, 2011)
okay...I now understand a bit more about the getResource, etc. When Norm first suggested that I got stuck. getResourceAsStream returns an InputStream, no problem, I got that. But then I needed to get that into an AudioInputStream and I didn't get very far. So I went to the web and found code using AudioInputStream and I thought that was what I needed, but obviously I was doing the same thing just a different way.
And now I see how it is to be done with the code you supplied Kevin, thank you
Thanks guys for all the help. It's just not "clicking" for me as fast as I would like it to.
Last edited by cl2606; June 7th, 2011 at 11:23 AM.
Turn on your speakers