在《
Java 載入Jar內資源問題的探究》這個文檔貼出來以后,有朋友給了我反饋,最終知道了問題就出現在JarOutputstream
輸出的時候,雖然支持直接寫入目錄中的文件來同時產生目錄和文件,但是這樣在jar中目錄就不是一個有效的entry,因此在資源定位的時候就無法得到,因此必須也把目錄作為entry寫入,這樣才會正常定位資源。這個問題作了測試以后反饋到我們的工具開發人員那邊,做了修改以后一切都恢復正常,細節決定成敗,那么一點細微的差異,會讓各種框架都無法正常運作。
代碼修改如下:
JarOutputStream jos;
try
{
jos = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
String f = "spring/sip-analyzer-dataSource.xml";
String dir = "spring/";
JarEntry je1 = new JarEntry(dir);
jos.putNextEntry(je1);
JarEntry je = new JarEntry(f);
jos.putNextEntry(je);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:/work/sip3/analyzer/src/conf.test/spring/sip-analyzer-dataSource.xml"));
int i = 0;
while ((i=bis.read())!=-1)
{
jos.write(i);
}
bis.close();
jos.closeEntry();
jos.close();
} catch ...