Search code examples
javafileembedded-resourcejavasound

music player "file not found" exception?


I'm try to add a soundtrack to a simple little java game, and I've researched how to do it to the extent that I have code that compiles. However, when I run it, I get a FileNotFound exception. I'm using Eclipse and my wav file is in the same directory as the .class files.

Here's my exception:

java.io.FileNotFoundException: bach.wav (No such file or directory)

And my code for the music player:

public static void play()
{
  try
  {
      Clip clip = AudioSystem.getClip();
      clip.open(AudioSystem.getAudioInputStream(new File("bach.wav")));
      clip.start();
  }
  catch (Exception exc)
  {
      exc.printStackTrace(System.out);
  }
 }

Solution

  • when you say new File("bach.wav") you have to put the file in the root folder of your project where the src and bin folders are