Web Development 的學習之旅

2008/06/04

Load resource file test / 讀取資源檔案測試

測試 function:
object.getClass().getResourceAsStream(filename);

Project 的資料夾結構:



結論:
* 只寫檔名只能找到與該 class 相同的 namespace 下的檔案.
* 若以相對路徑的寫法, 則 jar 檔內的檔案一律找不到. 即使在相同目錄也找不到.
* 以絕對路徑的寫法, 可以完全找到.


Project: ResTest1


package tw.purple.test;

import java.io.*;

public class ResTest1 {
public static void main(String[] args) {
ResTest1 test = new ResTest1();
test.runTest();
}

public void runTest() {
String[] pathList = {
""
,"./"
,"/"
,"../"
,"../../"
,"../../../"
,"/tw/"
,"/tw/purple/"
,"/tw/purple/test/"
,"/tw/yahoo/"
};
String[] nameList = {
"log4j_1.properties"
,"log4j_2.properties"
,"log4j_3.properties"
,"log4j_4.properties"
,"log4j_5.properties"
,"log4j_6.properties"
,"log4j_7.properties"
,"log4j_8.properties"
};

for (int i=0; i<pathList.length; i++) {
for (int j=0; j<nameList.length; j++) {
String filename = pathList[i] + nameList[j];
InputStream stream = this.getClass().getResourceAsStream(filename);
if (stream==null) System.out.print(" not found: ");
else System.out.print(" found: ");
System.out.println(filename);
}
}
}

}



Project: ResTest2

package tw.purple.test;

public class ResTest2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
ResTest1 test1 = new ResTest1();
test1.runTest();
}
}

2008/05/23

Eclipse 一些開發環境設定

[關掉 Spelling]
在編輯文字檔案時, Spelling 會進行檢查, 不合格的英文字下方就會有 underline 出現, 看起來挺礙事的, 自己是完全不會用上併字檢查的功能, 就關了它吧.
Windows->Perferences:
General->Editors->Text Editors->Spelling

[調整常用 Editor 字體大小]
Windows->Perferences:
General->Appearance->Colors and Fonts

Basic->Text Font: 調整一般文字檔的字體設定
Java->Java Editor Text Font: 調整 .java 檔案的字體設定
Java->Properties File Editor Text Font: 調整 .properties 檔案的字體設定
Debug->Console font: 調整 Console View 裡的文字大小

[Validation]
Eclipse 在很多時候會執行 Validation 的動作, 若機器等級不夠, 可是會讓人等翻天.
Windows->Perferences:
General->Validation 內有各種的 Validation, 可視需要把不需要的關掉, 或乾脆全關掉.
當然, 也可以至某 Project 內的 Properties 單獨設定 Validation 的開關.

Axis2 Eclipse plugin: Code Generator 在 Finish 時產生 Exception: java.lang.reflect.InvocationTargetException

[版本]
Eclipse 3.3.2
Axis2 1.4

[問題原因]
主要的問題在於 Code Generator plugin 內缺少了 backport-util-concurrent-3.1.jar 檔案

[解決方案]
- 關閉 Eclipse
- copy %AXIS2_HOME%\lib\backport-util-concurrent-3.1.jar 到下列 folder
%ECLIPSE_HOME%\plugins\Axis2_Codegen_Wizard_1.3.0\lib

- 註冊此 jar 檔案: 編輯 %ECLIPSE_HOME%\plugins\Axis2_Codegen_Wizard_1.3.0\plugin.xml , 在 <runtime> 內加入下面的字串
<library name="lib/backport-util-concurrent-3.1.jar">
<export name="*"/>
</library>

- 執行 Eclipse 即可

- 若問題仍存在, 嘗試改變 Eclipse 使用的 JRE, 換成 JAVA 6
- 使用 JRE 1.6 啟動 Eclipse 後, 若仍有問題, 試著加入 -clean 參數啟動 Eclipse, 清除 osig cache:
eclipse.exe -vm "c:\java\jre1.6.0_05\bin\java.exe" -clean -console -consoleLog -debug -vmargs -Xmx384M

- 關閉 Eclipse 後, 再以一般方式啟動 Eclipse, 應可解決問題

[參考]
Error while using Axis2 Code Generator Plugin for Eclipse
http://www.nabble.com/Error-while-using-Axis2-Code-Generator-Plugin-for-Eclipse-td17147595.html