我維護一個可幫助用戶將照片上傳到我們的服務的小程序。小程序jar
文件有幾個.properties
文件:如何在加載已經在applet jar文件中的屬性時阻止對服務器的請求?
>> jar -tf applet.jar | grep prop
res/messages.properties
res/messages_ca.properties
res/messages_es.properties
...
小程序初始化過程中,這些被加載。
messages = ResourceBundle.getBundle("res.messages");
此調用但是產生4至5對服務器的請求尋找那不在jar
文件中的文件,在回落至包括在.jar
的.properties
文件之前。
從服務器錯誤日誌:
[error] File does not exist: /photos/res/messages.class
[error] File does not exist: /photos/res/messages_en.class
[error] File does not exist: /photos/res/messages_en.properties
[error] File does not exist: /photos/res/messages_en_US.class
[error] File does not exist: /photos/res/messages_en_US.properties
爲ResourceManager.getBundle文檔解釋說,這是它做的方式:
的getBundle然後在候選包名稱迭代來發現第一爲此它可以實例化一個實際的資源包。對於每個候選包名稱,它試圖創建資源包:
首先,它嘗試使用候選包名稱加載一個類。如果可以使用指定的類加載器找到並加載這樣的類,那麼賦值與ResourceBundle兼容,可以從ResourceBundle訪問並且可以實例化,getBundle將創建此類的新實例並將其用作結果資源包。
否則,getBundle將嘗試查找屬性資源文件。它通過替換全部「。」來從候選包名中生成一個路徑名。帶有「/」的字符並附加字符串「.properties」。它嘗試使用ClassLoader.getResource查找具有此名稱的「資源」。
我肯定有很好的理由做這種方式,但對我來說,似乎浪費,我認爲應該有哪些是已知的文件五次失敗的請求不存在的服務器。
是否有任何方法教小程序只在.jar
文件中查找這些文件?
注意:我不是Java程序員,所以如果有更好的方法來加載屬性比ResourceManager.getBundle
,請讓我知道。
什麼是您的最低Java版本? – McDowell 2009-08-19 19:41:18
1.4以上是我們需要支持的。你的答案是否依賴於這些信息?無論如何請回復。如果您的解決方案對舊版本無效,那麼它仍然很有用,如果只是添加到原因列表中,我們應該停止支持1.4 :) – 2009-08-19 21:20:18