0

I am trying to get my .wav file to run from inside a .jar file once I have exported the program, and have researched extensively on the solution, but have found nothing that works.

Here is my current code:

        Clip clip = AudioSystem.getClip();
        clip.open(AudioSystem.getAudioInputStream(new File("soundfile.wav")));
        clip.start();

What can I do to fix this?

  • Mind sharing your extensive research and what you have tried? – awksp Jun 30 '14 at 23:42
  • stuff like URL (which doesnt even slighty work) and other things you can easily find on stackoverflow, but nothing that acutally works – user3672378 Jun 30 '14 at 23:43
  • 2
    Are you sure you have researched this solution extensively? If you had, you would have run into loading files from an embedded resource. – Vivin Paliath Jun 30 '14 at 23:44
  • And how does it "doesn't even slightly work"? You need to be more specific. Can you share the code you tried? – awksp Jun 30 '14 at 23:46

1 Answers1

1

A resource contained within the context of a Jar file is commonly known as an embedded resource.

These resources can not be accessed like normal file system resources (ie via the File class), instead, you need to use Class#getResource, which will return a URL, which can be, in you case, passed to AudioSystem.getAudioInputStream

You could use something like...

clip.open(AudioSystem.getAudioInputStream(getClass().getResource("/path/to/resources/soundfile.wav")));

In this example, the path is relative to the package location of the class instance. That means if the resource is in another directory, you will need to to provide a full path to the resource

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366