---
title: "jacob使用问题"
author: "lh8489"
date: "2009-09-17T11:22:39Z"
tags: [".net", "JDK", "JNI", "Struts", "Windows"]
slug: "jacob"
draft: false
lastmod: "2009-09-17T11:22:39Z"
id: 470302
url: "https://lh8489.iteye.com/blog/470302"
---

主要问题是使用的版本太低，jacobBin\_17，jacobBin\_16是我使用的两个版本，但是都调不通 。所以强烈建议下在最近的稳定版本，到官方网站上

[http://sourceforge.net/projects/jacob-project/](http://sourceforge.net/projects/jacob-project/)

我下载的是jacob\_1.12。

用的例子是[http://www.blogjava.net/galaxyp/archive/2007/06/21/125529.html](http://www.blogjava.net/galaxyp/archive/2007/06/21/125529.html)给出来的，原文及代码如下。

这个是csdn的编辑转发的一篇文章。

    jacob是java和windows下的com桥，通过它我们可以在java程序中调用COM组件。如果你的JDK是1.4，那你需要下载jacob1.9的jni库才能正常运行，早期版本在JDK1.4下有些问题。

```
package com;
/**
 * <p>Title:Word文档转html类</p>
 * <p>Description: </p>
 * <p>Copyright:() 2002</p>
 * @author 舵手
 * @version 1.0
 */
import com.jacob.com.*;
import com.jacob.activeX.*;

public class WordtoHtml {
 /**
  *文档转换函数
  *@param docfile word文档的绝对路径加文件名(包含扩展名)
  *@param htmlfile 转换后的html文件绝对路径和文件名(不含扩展名)
  */
 public static void change(String docfile, String htmlfile) {
  ActiveXComponent app = new ActiveXComponent("Word.Application");// 启动word
  try {
   app.setProperty("Visible", new Variant(false));
    //设置word不可见
   Object docs = app.getProperty("Documents").toDispatch();
   Object doc = Dispatch.invoke(docs,"Open",Dispatch.Method,new Object[] { docfile, new Variant(false),new Variant(true) }, new int[1]).toDispatch();
   // 打开word文件
   Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {htmlfile, new Variant(8) }, new int[1]);
   // 作为html格式保存到临时文件
   Variant f = new Variant(false);
   Dispatch.call(doc, "Close", f);
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   app.invoke("Quit", new Variant[]{});
  }
 }
 public static void main(String[] strs){
  WordtoHtml.change("c:\\a\\运输管理调度系统总体方案.doc", "c:\\a\\t");
  
 }
}

```

    
由于开始使用的16,17两个版本支持的jdk过低，所以总抱错，我用的是jdk1.6,所以下载最新的jacom，

16，17应该是4，5年前的版本了。仅支持jdk1.3，当然有很多问题。

错误1（16版本的报的错）：com.jacob.com.comfailexception    
  com.jacob.com.comfailexception:   a   com   exception   has   been   encountered:    
  at   invoke   of:   documents    
  description:   an   unknown   com   error   has   occured.    
  at   com.jacob.com.dispatch.invokev(native   method)    
  at   com.jacob.com.dispatch.get(dispatch.java)    
  at   struts\_temp.wordbean.createnewdocument(wordbean.java:29)    
  at   struts\_temp.wordtest.main(wordtest.java:12)  

错误2（17版本的报的错）：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"  
换成最新的之后，两个错误就没有了，看到许多文章给出来的解决方法是使用以前的jdk1.3,简直就是倒退。

 不过使用最新的jacom上面例子中的代码会报错：将Object改为Dispatch 即可解决。

```
public class WordToHtml {
 /**
 *文档转换函数
 *@param docfile word文档的绝对路径加文件名(包含扩展名)
 *@param htmlfile 转换后的html文件绝对路径和文件名(不含扩展名)
 */
 public static void change(String docfile, String htmlfile) {
 ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word
 try {
 app.setProperty("Visible", new Variant(false)); 
// 设置word不可见
 Dispatch docs = app.getProperty("Documents").toDispatch(); 
 Dispatch doc = Dispatch.invoke(docs,"Open",Dispatch.Method,new Object[] { docfile, new Variant(false),new Variant(true) }, new int[1]).toDispatch(); 
//  打开word文件
 Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {htmlfile, new Variant(8) }, new int[1]); 
//  作为html格式保存到临时文件
 Variant f = new Variant(false); 
 Dispatch.call(doc, "Close", f); 
 } catch (Exception e) {
 e.printStackTrace(); 
 } finally {
 app.invoke("Quit", new Variant[]{}); 
 }
 }
 public static void main(String[] strs){
  WordToHtml.change("D:\\ghr\\jacob\\test.doc", "D:\\ghr\\jacob\\t"); 

 }


}

```

 

配置问题，

jacob\_1.12\\x86\\jacom.dll文件拷到C:\\WINDOWS\\system32下面即可，当然也需要将jar包放进去

 

本文来自CSDN博客，转载请标明出处：[http://blog.csdn.net/g089h515r806/archive/2007/06/23/1662763.aspx](http://blog.csdn.net/g089h515r806/archive/2007/06/23/1662763.aspx)