0

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:

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.

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?

4 Answers4

1

First thing that goes into my mind is that you didn't attached your sound library classes into your jar.

MGorgon
  • 2,547
  • 23
  • 41
  • Sorry but I am new to java and dont really understand what you mean by "attached your sound library classes into your jar" shouldnt that be done by eclipse automatically? + no errors are thrown, if the errors were thrown i would have seen them in the CMD. – lifedistroy Jul 31 '13 at 19:20
1

In order to run your current code, the folder music should be in the same folder the jar file is located in.

Another solution is to package your music folder inside the jar file and then change your code to:

InputStream is = getClass().getResourceAsStream("/music/" + name[x]);
AudioInputStream sound = AudioSystem.getAudioInputStream(is);
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
1

How about-

  1. Right click on your project in Eclipse. Then New -> Source Folder.
  2. Name the source folder anything. e.g. music_src.
  3. Copy or drag the entire music directory in music_src. Then make the jar.
Sajal Dutta
  • 18,272
  • 11
  • 52
  • 74
0

File systems have a hard time looking into jars. Try using URL instead. A URL can locate a location within a jar. This happens a lot with folks trying to access resources in jars for the first time. Otherwise things look fine.

Phil Freihofner
  • 7,645
  • 1
  • 20
  • 41