java: 关于从jar中读取资源遇到的问题getClass().getResource(...)

在Java的程序发布中,很多人会选择采用二进制的jar的格式进行发布,怎么样读取Jar里面的资源呢?
主要是采用ClassLoader的下面几个方法来实现:
public URL getResource(String name);
public InputStream getResourceAsStream(String name)

public static InputStream getSystemResourceAsStream(String name)
public static URL getSystemResource(String name)

我一般都是使用:

getClass().getResource(...);

比如,设置JFrame的图标:

this.setIconImage(new ImageIcon(getClass().getResource(...)).getImage());

但是我今天遇到的问题是,在程序中需要播放一个存放在jar中的notify.wav声音文件,我的代码:

System.out.println(getClass().getResource("/sound/notify.wav").getPath());
System.out.println(getClass().getResource("/sound/notify.wav").getFile());
System.out.println(getClass().getResource("/sound/notify.wav").toExternalForm());
System.out.println(getClass().getResource("/sound/notify.wav").toString());  

/*

//输出的路径

/D:/soft/test/bin/sound/notify.wav
/D:/soft/test/bin/sound/notify.wav
file:/D:/soft/test/bin/sound/notify.wav
file:/D:/soft/test/bin/sound/notify.wav

*/  

FileInputStream audioFIS = new   FileInputStream(getClass().getResource("/sound/notify.wav").getFile());

AudioPlayer.player.start(audioFIS); //播放声音

在eclipse中测试的时候,能正常播放声音,但打包成jar文件后,就提示找不到声音文件了。 

为什么在最上面的设置窗口的图标时,可以,但在这里播放jar中的声音文件时就出错了呢?

最后通过在网上搜索,找到这篇介绍(http://www.iteye.com/topic/483115)才明白:

摘抄一部分内容:...........................

这主要是因为jar包是一个单独的文件而非文件夹,绝对不可能通过"file:/e:/.../ResourceJar.jar/resource /res.txt"这种形式的文件URL来定位res.txt。所以即使是相对路径,也无法定位到jar文件内的txt文件(读者也许对这段原因解释有些费解,在下面我们会用一段代码运行的结果来进一步阐述)。

 因为".../ResourceJar.jar!/resource/...."并不是文件资源定位符的格式 (jar中资源有其专门的URL形式: jar:<url>!/{entry} )。所以,如果jar包中的类源代码用File f=new File(相对路径);的形式,是不可能定位到文件资源的。这也是为什么源代码1打包成jar文件后,调用jar包时会报出FileNotFoundException的症结所在了。

//解决办法也很简单,直接使用:getClass().getResourceAsStream()

InputStream audioIS = getClass().getResourceAsStream("/sound/notify.wav");;
AudioPlayer.player.start(audioIS);

2012-01-29

原文地址:https://www.cnblogs.com/personnel/p/4583174.html