如何避免java.lang.NoClassDefFoundError

我有一个代码,用于将文本添加到现有的.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.(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并不意味着您拥有该框架的所有类。


根据我的评论(或其他人的答案)解决问题后,您有了这个新的例外

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

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

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

在此处输入图像描述

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

确保项目的类路径中包含所有jar。

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

  org.apache.poi ooxml-schemas 1.3  

要么

 System couldn't find the 

POI-OOXML-架构 – xx.xx.jar

请将库添加到类路径中。

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

您收到该错误是因为您没有XWPFDocument的正确依赖项。 ooxml-schemas需要xmlbeans,而ooxml需要poi和ooxml-schemas等…

点击此处: http : //poi.apache.org/overview.html#components

以为我会报告我的错误经验。 我开始明白了,并没有在我的工作区中改变任何东西。 事实certificate,它在尝试读取超过1张的Excel文件时出现(第二张是数据透视表,大量数据。不确定是否由于数据的大小(我怀疑是这样,因为我已经读取了包含多个工作表的Excel文件。)当我删除第二张工作表时,它工作正常。不需要更改类路径。

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

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

FWIW我不得不补充一下:

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

对于我的情况,我有不同版本的poi(s)。 poi-scratchpad是3.9,其他所有 – poi,poi-ooxml,poi-ooxml-schemas是3.12。 我将poi-scratchpad的版本改为3.12,一切都开始工作了。

如果您没有使用maven作为项目依赖项。 您应该在类路径中包含以下jar 在此处输入图像描述