Anthony1617
Anthony1617

Reputation: 47

Music plays when running program in Eclipse, but not when running Jar executable

When I run my program in Eclipse, the music runs just fine when I access the part of the program that runs it. When I compile it as a jar, however, the music does not play, and everything else works fine.

I am making a 2d side scroller game, and have a class for each level. In the class for the first level, I have this code in the constructor:

LoadClip();

and this method later on in the class:

private void LoadClip() {
    Clip c = null;
        try {
            AudioInputStream music = AudioSystem.getAudioInputStream(getClass().getResourceAsStream("wily.wav"));
            c = AudioSystem.getClip();
            c.open(music);
            c.start();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
}

Upvotes: 3

Views: 99

Answers (1)

Sam Yuyitung
Sam Yuyitung

Reputation: 71

The issue is that you cannot read a resource as a stream from a jar

to solve: replace getResourceAsStream with getResource

like so:

AudioInputStream music = AudioSystem.getAudioInputStream(getClass().getResource("wily.wav"));

Hope this helps!

Upvotes: 2

Related Questions