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.

Results 1 to 18 of 18

Thread: no sound from executable jar file

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default no sound from executable jar file

    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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: no sound from executable jar file

    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.

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: no sound from executable 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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: no sound from executable jar file

    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?

  5. #5
    Junior Member
    Join Date
    Jun 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: no sound from executable jar file

    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)

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: no sound from executable jar file

    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

  7. #7
    Junior Member
    Join Date
    Jun 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: no sound from executable jar file

    No problem. Thanks for the help.

    Curt

  8. #8
    Junior Member
    Join Date
    Jun 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: no sound from executable jar file

    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

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: no sound from executable jar file

    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.

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: no sound from executable jar file

    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!

  11. #11
    Junior Member
    Join Date
    Jun 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: no sound from executable jar file

    Quote Originally Posted by Norm View Post
    How did you test it on your computer? Did you move the jar file to an empty test folder?
    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

  12. #12
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: no sound from executable jar file

    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!

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: no sound from executable jar file

    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

  14. The Following User Says Thank You to Norm For This Useful Post:

    KevinWorkman (June 7th, 2011)

  15. #14
    Junior Member
    Join Date
    Jun 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: no sound from executable jar file

    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

  16. #15
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: no sound from executable jar file

    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!

  17. The Following User Says Thank You to KevinWorkman For This Useful Post:

    cl2606 (June 7th, 2011)

  18. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: no sound from executable jar file

    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.

  19. The Following User Says Thank You to Norm For This Useful Post:

    cl2606 (June 7th, 2011)

  20. #17
    Junior Member
    Join Date
    Jun 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: no sound from executable jar file

    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.

  21. #18
    Banned
    Join Date
    Jun 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: no sound from executable jar file

    Turn on your speakers

Similar Threads

  1. Jar Executable File Not Working
    By Kimimaru in forum Java Theory & Questions
    Replies: 6
    Last Post: October 15th, 2010, 09:32 PM
  2. Jar Executable File
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 43
    Last Post: June 23rd, 2010, 03:53 PM
  3. [SOLVED] Executable .jar file isn’t launched after being double-clicked
    By voltaire in forum Java Theory & Questions
    Replies: 6
    Last Post: May 18th, 2010, 03:37 PM
  4. outputing to a text file from sound API
    By bondage in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: May 2nd, 2010, 11:43 AM
  5. Making executable JAR more "executable"
    By ni4ni in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 1st, 2010, 01:19 PM