【问题标题】:How to avoid java.lang.NoClassDefFoundError如何避免 java.lang.NoClassDefFoundError
【发布时间】:2013-02-12 22:30:51
【问题描述】:

我有一个用于将文本添加到现有 .doc 文件的代码,它会使用 apache POI 将其另存为另一个名称。

以下是我目前尝试过的代码

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFFooter;
import org.apache.poi.xwpf.usermodel.XWPFTable;

public class FooterTableWriting {

    public static void main(String args[])
    {
        String path="D:\\vignesh\\AgileDocTemplate.doc";
        String attch="D:\\Attach.doc";
        String comment="good";
        String stat="ready";
        String coaddr="xyz";
        String cmail="abc@gmail.com";
        String sub="comp";
        String title="Globematics";
        String cat="General";
        setFooter(path, attch, comment, stat, coaddr, cmail, sub, title, cat);
    }
    private static  void setFooter(String docTemplatePath,String attachmentPath,String comments,String status,String coAddress,String coEmail,String subject,String title,String catagory)
    {
          try{

                    InputStream input = new FileInputStream(new File(docTemplatePath));
                    XWPFDocument document=new XWPFDocument(input);
                    XWPFHeaderFooterPolicy headerPolicy =new XWPFHeaderFooterPolicy(document);
                    XWPFFooter footer = headerPolicy.getDefaultFooter();
                    XWPFTable[] table = footer.getTables();

                    for (XWPFTable xwpfTable : table)
                       {
                           xwpfTable.getRow(1).getCell(0).setText(comments);
                           xwpfTable.getRow(1).getCell(1).setText(status);
                           xwpfTable.getRow(1).getCell(2).setText(coAddress);
                           xwpfTable.getRow(1).getCell(3).setText(coEmail);
                           xwpfTable.getRow(1).getCell(4).setText(subject);
                           xwpfTable.getRow(1).getCell(5).setText(title);
                           xwpfTable.getRow(1).getCell(6).setText(catagory);

                       }

                  File f=new File (attachmentPath.substring(0,attachmentPath.lastIndexOf('\\')));

                  if(!f.exists())
                      f.mkdirs();

                  FileOutputStream out = new FileOutputStream(new File(attachmentPath));
                  document.write(out);
                  out.close();

                  System.out.println("Attachment Created!");

         }
          catch(Exception e)
          {
              e.printStackTrace();
          }

    }

}

以下是我得到的

    org.apache.poi.POIXMLException: org.apache.xmlbeans.XmlException: error: The document is not a document@http://schemas.openxmlformats.org/wordprocessingml/2006/main: document element mismatch got themeManager@http://schemas.openxmlformats.org/drawingml/2006/main
    at org.apache.poi.xwpf.usermodel.XWPFDocument.onDocumentRead(XWPFDocument.java:124)
    at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:200)
    at org.apache.poi.xwpf.usermodel.XWPFDocument.<init>(XWPFDocument.java:74)
    at ext.gt.checkOut.FooterTableWriting.setFooter(FooterTableWriting.java:32)
    at ext.gt.checkOut.FooterTableWriting.main(FooterTableWriting.java:25)
Caused by: org.apache.xmlbeans.XmlException: error: The document is not a document@http://schemas.openxmlformats.org/wordprocessingml/2006/main: document element mismatch got themeManager@http://schemas.openxmlformats.org/drawingml/2006/main
    at org.apache.xmlbeans.impl.store.Locale.verifyDocumentType(Locale.java:458)
    at org.apache.xmlbeans.impl.store.Locale.autoTypeDocument(Locale.java:363)
    at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1279)
    at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1263)
    at org.apache.xmlbeans.impl.schema.SchemaTypeLoaderBase.parse(SchemaTypeLoaderBase.java:345)
    at org.openxmlformats.schemas.wordprocessingml.x2006.main.DocumentDocument$Factory.parse(Unknown Source)
    at org.apache.poi.xwpf.usermodel.XWPFDocument.onDocumentRead(XWPFDocument.java:92)
    ... 4 more

我已经添加了与此相对应的所有 jar 文件,但我仍然找不到解决方案。我是这个 apache poi 的新手,所以请帮助我一些解释和示例。 谢谢

【问题讨论】:

  • 看起来您需要 Apache POI 发行版中的 poi-ooxml-schemas.jar。仅仅添加一个 jar 并不意味着你拥有了框架的所有类。
  • 你在使用 Eclipse 吗?
  • 是的....我正在使用 eclipse
  • 如何避免?像往常一样,确保所有必需的类都在类路径中;)
  • lol @EJP 如果您真的阅读了 cmets,操作人员说他通过添加一个额外的 jar 修复了第一件事,现在他得到了一个不同的错误。然后他说他会更新错误。我希望我可以对您的评论投反对票。

标签: java apache-poi noclassdeffounderror doc


【解决方案1】:

org.openxmlformats.schemas.wordprocessingml.x2006.main.DocumentDocument.Factory 类位于 jar ooxml-schemas-1.0.jar 中,可以下载 here

【讨论】:

    【解决方案2】:

    这是poi-ooxml-3.9.jar 的依赖层次结构。

    这意味着它们中的任何一个都可以在运行时使用,即使它们没有在编译时使用。

    确保在项目的类路径中拥有所有 jar。

    【讨论】:

    • 我有以下结构。但我有 poi-ooxml 3.8 和 poi-ooxml-schemas 3.8
    • 拥有不同的版本应该不会导致任何问题。
    【解决方案3】:

    从我对问题的评论中复制:

    看起来您需要 Apache POI 发行版中的 poi-ooxml-schemas.jar。只添加一个 jar 并不意味着您拥有框架的所有类。


    根据我的评论(或其他人的回答)解决问题后,您有这个新异常

    org.apache.xmlbeans.XmlException: error: The document is not a document@http://schemas.openxmlformats.org/wordprocessingml/2006/main: document element mismatch got themeManager@http://schemas.openxmlformats.org/drawingml/2006/main
    

    阅读Apache POI - HWPF - Java API to Handle Microsoft Word Files,您似乎使用了错误的类来处理 2003-word 文档:HWPF 是我们将 Microsoft Word 97(-2007) 文件格式转换为纯 Java 的端口的名称 ... 新的 Word 2007 .docx 格式的 HWPF 的合作伙伴是 XWPF。。这意味着您需要 HWPFDocument 类来处理文档或将文档从 Word 2003- 更改为 Word 2007+。

    IMO 我发现 Apache POI 是处理 Excel 文件的一个很好的解决方案,但我会寻找另一种处理 Word 文档的选项。查看this question以获取更多相关信息。

    【讨论】:

      【解决方案4】:

      您收到该错误是因为您没有适当的 XWPFDocument 依赖项。 ooxml-schemas 需要 xmlbeans,ooxml 需要 poi 和 ooxml-schemas 等等...

      在这里查看:http://poi.apache.org/overview.html#components

      【讨论】:

        【解决方案5】:

        我想我会报告我遇到此错误的经历。我开始突然发现它,并且没有改变我的工作区中的任何东西。事实证明,它发生在尝试读取具有超过 1 张工作表的 Excel 文件时(第二张工作表是数据透视表,大量数据。不确定是否是由于数据的大小造成的(我怀疑是这样,因为我已阅读包含超过 1 个工作表的 Excel 文件)。当我删除第二张工作表时,它起作用了。无需更改类路径。

        【讨论】:

          【解决方案6】:

          org.apache.poi.POIXMLException: org.apache.xmlbeans.XmlException: Element themeManager@http://schemas.openxmlformats.org/drawingml/2006/main 不是有效的工作簿@http://schemas.openxmlformats.org/spreadsheetml/2006/main 文档或有效的替换。

          解决方案:- 使用 .xlsx 格式而不是 .xls

          【讨论】:

            【解决方案7】:

            FWIW 我不得不添加这个:

            compile 'org.apache.poi:ooxml-schemas:1.3'
            

            【讨论】:

              【解决方案8】:

              就我而言,我有不同版本的 poi。 poi-scratchpad 为 3.9,所有其他 - poi、poi-ooxml、poi-ooxml-schemas 为 3.12。我也将 poi-scratchpad 的版本更改为 3.12,一切都开始工作了。

              【解决方案9】:

              在您的配置文件中添加此依赖项:

              <dependency>
                  <groupId>org.apache.poi</groupId>
                  <artifactId>ooxml-schemas</artifactId>
                  <version>1.3</version>
              </dependency>
              

              System couldn’t find the 
              

              poi-ooxml-schemas-xx.xx.jar

              请将该库添加到您的类路径中。

              【讨论】:

                【解决方案10】:

                如果您的项目依赖项没有使用 maven。您的类路径中应该有以下 jar

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2019-01-02
                  • 2010-12-07
                  • 1970-01-01
                  • 2011-05-15
                  • 2014-05-30
                  • 2022-01-26
                  • 2014-10-15
                  • 2013-07-16
                  相关资源
                  最近更新 更多