jacob配置方法及兩個常見錯誤解決
通過Jacob可以很方便的通過編程操作Word等MS組件,以前我也寫過一篇關於如何使用Jacob進行將word文檔轉為html和其他格式的文檔(
http://blog.csdn.net/guoxiaoshou2000/archive/2004/12/07/208024.aspx
);而且附上了源碼。
但是最近有些朋友跟我聯絡,說無法正確執行我提供的代碼,提示的錯誤如下:
com.jacob.com.ComFailException: Invoke Failed: Documents at com.jacob.com.Dispatch.invokev(Native Method) at
com.jacob.activeX.ActiveXComponent.getProperty(ActiveXComponent.java) at wordtohtml.change(wordtohtml.java:62)
at wordtohtml.main(wordtohtml.java:104)
com.jacob.com.ComFailException: Invoke Failed: Quit
at com.jacob.com.Dispatch.invokev(Native Method)
at com.jacob.activeX.ActiveXComponent.invoke(ActiveXComponent.java) at wordtohtml.change(wordtohtml.java:77) at wordtohtml.main(wordtohtml.java:104)
Exception in thread "main"
這個錯誤以前我也碰到過,在這裡跟將我的解決方案說明一下。
造成這種錯誤的一種原因是jdk與你的jacob版本不相符合,如果使用jacob1.6而使用jdk1.4或以上版本,那麼肯定會出這種錯誤,解決方案可以升級你的jacob.dll版本——這個在yahoo的jacob討論群組上面可以有下載檔案名稱是:jacob_jre142fix_bin。
另外注意的是,這樣升級後仍然可能出錯,這樣的話,建議採用下面的配置方法。
jdk1.3.1_15 + jacob.jar 1.7 + jacob.dll的jacob_jre142fix_bin版本
其中jdk可以直接從sun的網站下載,下載連結為:
http://192.18.97.50/ECom/EComTicketServlet/BEGINBB924C3A202206BE2FD20BEBE583F482/-2147483648/829060395/1/557666/557654/829060395/2ts+/westCoastFSEND/j2sdk-1.3.1_15-oth-JPR/j2sdk-1.3.1_15-oth-JPR:7/j2sdk-1_3_1_15-windows-i586.exe
注意,下載完畢以後是一個檔案名稱很長的檔案,將其改名為jdk1.3.1_15.exe即可。
jacob.jar 1.7的如下:
http://danadler.com/jacob/jacobSrc_17.zip
注意,不要使用它裡面提供的jacob.dll。
jacob.dll可從yahoo的jacob討論群組下載(呵呵,給它做廣告了)。
下載完畢所有的檔案以後,將jacob.jar解壓然後將com和META-INF中的檔案分別添加到jdk的lib中的tools.jar中的對應檔案夾中(如果不想要這樣,通過環境變數設定也可以,但是嘗容易出錯),而jacob.dll則可以直接放到system32中即可。
這裡還要注意一點,如果你的jacob.jar和jacob.dll的版本不一致的話,很可能出現下面的錯誤:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no jacob in java.libr
ary.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1344)
at java.lang.Runtime.loadLibrary0(Runtime.java:744)
at java.lang.System.loadLibrary(System.java:815)
at com.jacob.com.Dispatch.<clinit>(Dispatch.java)
at openword.open(openword.java:12)
at openword.main(openword.java:48)
或者:
Exception in thread "main" java.lang.UnsatisfiedLinkError: doCoInitialize
at com.jacob.com.ComThread.doCoInitialize(Native Method)
at com.jacob.com.ComThread.Init(ComThread.java)
at com.jacob.com.ComThread.InitMTA(ComThread.java)
at com.jacob.com.ROT.addObject(ROT.java)
at com.jacob.com.JacobObject.<init>(JacobObject.java)
at com.jacob.com.Dispatch.<init>(Dispatch.java)
at com.jacob.activeX.ActiveXComponent.<init>(ActiveXComponent.java)
at openword.open(openword.java:12)
at openword.main(openword.java:48)
這裡要特別注意這一點。
好了,到這裡所有的配置就完畢了,以上就是我的一些經驗了,如果有不對的地方請大家到我的blog(http://blog.csdn.net/guoxiaoshou2000)告訴我。
上面的方法已經經過我個人測試,這樣配置的話,應該是沒有問題了——不過如果你不能用jdk1.3那可以自己實驗其他的辦法,我這邊有時間也會繼續研究的。祝大家好運!