Audio does not play in Jar but does in eclipse

时光总嘲笑我的痴心妄想 提交于 2019-12-02 10:17:18

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

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);

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!