Audio does not play in Jar but does in eclipse

前端 未结 4 1973
眼角桃花
眼角桃花 2021-01-27 08:36

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         


        
相关标签:
4条回答
  • 2021-01-27 09:14

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

    0 讨论(0)
  • 2021-01-27 09:14

    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);
    
    0 讨论(0)
  • 2021-01-27 09:30

    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.

    0 讨论(0)
  • 2021-01-27 09:36

    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.
    0 讨论(0)
提交回复
热议问题