lifedistroy
lifedistroy

Reputation: 21

sound will play in eclipse but not in exported jar file

Sound does not play when I run the JAR, but it does when I run it in eclipse.

Here is where I load the clips: (the files are loaded from the directory of the jar, Not from within the jar)

public void init(){
    System.out.println("grabbing Music");
    String currentDir = new File("").getAbsolutePath();
    name=new File(currentDir+"\\music\\").list();
    clip=new Clip[name.length];
    soundFile=new File[name.length];
    for(int x=0;x<name.length;x++){
        System.out.println(currentDir+"\\music\\"+name[x]);
        try {
            soundFile[x]= new File(currentDir+"\\music\\"+name[x]);
            AudioInputStream sound = AudioSystem.getAudioInputStream(soundFile[x]);
            DataLine.Info info= new DataLine.Info(Clip.class, sound.getFormat());
            clip[x] = (Clip) AudioSystem.getLine(info);
            clip[x].open(sound);
            clip[x].addLineListener(new LineListener(){
                public void update(LineEvent event) {
                    if (event.getType() == LineEvent.Type.STOP) {
                        event.getLine().close();
                    }
                }
            });
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
        }
    }

}

I do not get any errors when running it in Eclipse. There should be no possibility of an invalid directory error, so what is wrong?

-When the jar is run in CMD i get no errors nor stacktraces, so it IS loading the files, it just isnt running it when the clip.start() is called. So the way im loading the files in is not compatible with runnable jars.

edit: I feel like I am loading the audio wrong, hence why I pasted the code I used to load the files in. In my searches I haven't seen anyone use File to load in a sound file. Wonder if that is the problem?

-Also switching to embedded resources ALSO does not work.

Upvotes: 2

Views: 2502

Answers (2)

BigSauce
BigSauce

Reputation: 1840

I struggled with this as well. You need to use

getClass().getSystemResource("pathToResourceFromClass")

For example, if your project structure is like the following:

-ProjectName

-----src

----------SomeClass.java

-----images

----------SomeImage.png

Then from SomeClass.java, you would use

getClass().getSystemResource("images/SomeImage.png")

to obtain the URL object which references that file. You could then pass that URL object to the constructor of ImageIcon if you want.

Note that getClass().getResource() does not work for images or sound files in a jar file!!! I've spent many hours on this, and getResource() does not work. You have to use getSystemResource(..) for resources within a jar file.

Let me know if you have any questions about this. I'd be glad to clarify any confusing points.

Upvotes: 2

Mark Nutt
Mark Nutt

Reputation: 358

The reason your sound is not playing because it is most likely not being compiled into your runnable jar and thus cannot be found. The best way to have it compile into a runnable jar is to create a source folder in eclipse, and then add your sound file in there. It will then be compiled into your jar

Upvotes: 2

Related Questions