如何避免 java.lang.NoClassDefFoundError [英] How to avoid java.lang.NoClassDefFoundError

查看:43
本文介绍了如何避免 java.lang.NoClassDefFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a code for adding the texts to existing .doc file and it'll save that as another name by using apache POI.

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

The following is the code I have tried so far

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 的新手,所以请帮助我提供一些解释和示例.谢谢

I have added all the jar files corresponding to this but still I can't find the solution.I'm new to this apache poi so please help me with some explanations and examples. Thanks

推荐答案

复制自我对问题的评论:

Copied from my comment done to the question:

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

Looks like you need poi-ooxml-schemas.jar that comes in the Apache POI distribution. Just adding a single jar doesn't mean that you have all the classes of the framework.

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

After solving the problem based on my comment (or another people answers), you have this new Exception

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 来处理 Microsoft Word 文件,它看起来您使用了错误的类来处理 2003-word 文档:HWPF 是我们将 Microsoft Word 97(-2007) 文件格式移植到纯 Java 的端口名称 ... 新 Word 2007 .docx 格式的 HWPF 的合作伙伴是 XWPF..这意味着您需要 HWPFDocument 类来处理文档或将文档从 Word 2003- 更改为 Word 2007+.

Reading Apache POI - HWPF - Java API to Handle Microsoft Word Files, it looks like you're using the wrong class to handle 2003- word documents: HWPF is the name of our port of the Microsoft Word 97(-2007) file format to pure Java ... The partner to HWPF for the new Word 2007 .docx format is XWPF.. This means that you need HWPFDocument class to handle the document or change your document from Word 2003- to Word 2007+.

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

IMO I find Apache POI as a good solution to handling Excel files, but I would look another options to handling Word documents. Check this question to get more related info.

这篇关于如何避免 java.lang.NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆