最近在写一个基于JAVA SOCKET的局域网聊天程序,以UI为表现形式,可以传输文件,支持远程控制。

先来看一下IMPower目录结构:

src

    -ControlLayer

    -DaoLayer

    -ModelLayer

    -ViewLayer

images

    images.jpg

sound

    newMsg.wav

未用到打包时,如果我们要使用到images下的images.jpg的话,只需要ImageIcon img = new ImageIcon("images/images.jpg");这样就正确地完成了资源的导入过程。

Jar打包资源时,如果还是这么写的话,会出现资源找不到,我们假设JAR打包的话,目录结构是这样的:

 ControlLayer

 DaoLayer

 ModelLayer

 ViewLayer

 images.jpg

 newMsg.wav

ImageIcon img = new ImageIcon(this.getClass().getResource("/images.jpg"));这样就能正确定位images.jpg的位置了。注意,我们这时用到的java搜索路径是以类为基准的,而不依赖当前路径。