How to avoid java.lang.NoClassDefFoundError

烈酒焚心 提交于 2019-11-29 07:21:24
Luiggi Mendoza

Copied from my comment done to the question:

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

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 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.

This is the dependency hierarchy for poi-ooxml-3.9.jar.

Which means any of them can be used at runtime even if they aren't used at compile-time.

Make sure you have all the jars in the classpath of your project.

Add this dependency on your config file:

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

or

System couldn’t find the 

poi-ooxml-schemas-xx.xx.jar

Please add the library to your classpath.

The class org.openxmlformats.schemas.wordprocessingml.x2006.main.DocumentDocument.Factory is located in the jar ooxml-schemas-1.0.jar which can be downloaded here

You're getting that error because you don't have the proper dependency for the XWPFDocument. ooxml-schemas requires xmlbeans, and ooxml requires poi and ooxml-schemas, etc...

Check here: http://poi.apache.org/overview.html#components

Thought I would report my experience with this error. I started getting it out of the blue, and hadn't changed anything in my workspace. Turns out that it occurs while trying to read an Excel file that has more than 1 sheet (second sheet was a pivot table, large amount of data. Not quit sure if it's due to the size of the data (I suspect so, because I HAVE read Excel files that contain more than 1 worksheet). When I deleted that second sheet, it worked. No changes to classpath needed.

org.apache.poi.POIXMLException: org.apache.xmlbeans.XmlException: Element themeManager@http://schemas.openxmlformats.org/drawingml/2006/main is not a valid workbook@http://schemas.openxmlformats.org/spreadsheetml/2006/main document or a valid substitution.

Solution :- use .xlsx format instead of .xls

FWIW I had to add this:

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

For my case I had different versions of poi(s). poi-scratchpad was of 3.9 and all others - poi, poi-ooxml,poi-ooxml-schemas were of 3.12. I changed version of poi-scratchpad to 3.12 as well and everything started working.

If you are not using maven for your project dependencies. You should have the following jars in your classpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!