0

I have a program which has to play sounds from a terminal interface.

The code is fairly simple and here it is :

public static synchronized void playSound() {
    new Thread(new Runnable() {
        public void run() {
            File _file = new File("music/sound.wav");

            try (AudioInputStream _audio = AudioSystem
                    .getAudioInputStream(_file)) {
                Clip _clip = AudioSystem.getClip();
                _clip.open(_audio);
                _clip.start();
            } catch ([…] e) {
                // […]
            }
        }
    }).start();        
}

The file is in the music folder which is in my source path.

All work perfectly well when I run the program in eclipse. But if I export it in a .jar file and try it in the windows cmd I get this message :

java.io.FileNotFoundException: music\sound.wav (The system cannot find the path specified)

[edit] The audio files are indeed packed into the .jar, but it still doesn’t work.

Is it even possible to play a sound from the windows prompt? If not, is there one that does?

Thanks, SilverDuck

Silver Duck
  • 581
  • 1
  • 5
  • 18
  • Where is this music folder relative to where you're running it from? – Evan Knowles May 09 '14 at 09:31
  • Your issue is the relative filepath `music/sound.wav`. I imagine, when you've created your jar, you haven't included the audio file within the jar. Of course, you could build your jar with the required resource, but why not make your application accept the filepath as an input? Then you could provide the full path when you run the jar and it could pick the file up. This way, you could play any file you provided. – Dan Temple May 09 '14 at 09:39
  • The music folder is bundled into the jar file, that’s why I don’t understand. When I don’t bundle it I have another error saying that the path doesn’t exists. – Silver Duck May 09 '14 at 09:44
  • Hmm, odd. There's not something funky happening with the file separator is there? You code has `music/sound.wav`, but then the error message has `music\sound.wav`. – Dan Temple May 09 '14 at 09:54
  • Yes, java uses / as a separator and windows \, but I think the jvm takes care of it. – Silver Duck May 09 '14 at 09:55

2 Answers2

0

When the file is packaged into a jar file, it is no longer a File. It needs to be read as a resource. Try changing the code like this

InputStream inputStream = this.getClass().getResourceAsStream("music/sound.wav");
 try (AudioInputStream _audio = AudioSystem.getAudioInputStream(inputStream)) {
Keerthivasan
  • 12,760
  • 2
  • 32
  • 53
  • Yes, that I already did, but it still won’t work. If I open my .jar with a zip manager, I can see the files were they are should be. – Silver Duck May 09 '14 at 09:51
0

Try either not packaging your music in jar (put it alongside) or load your packaged file as a resource.

See Java resource files for example.

Here Loading resources (images) contained in a .Jar file or in the classpath might be a better explanation.

Community
  • 1
  • 1
enlait
  • 607
  • 1
  • 6
  • 19