5

I'm trying to make work a piece of code to read a docx file, but it fails for some strange reason.

It fails on this line:

XWPFDocument document = new XWPFDocument(new FileInputStream(filepath));

Stack trace:

Exception in thread "main" java.lang.NoSuchMethodError: org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl.getTrList()Ljava/util/List;
    at org.apache.poi.xwpf.usermodel.XWPFTable.<init>(XWPFTable.java:106)
    at org.apache.poi.xwpf.usermodel.XWPFDocument.onDocumentRead(XWPFDocument.java:151)
    at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:159)
    at org.apache.poi.xwpf.usermodel.XWPFDocument.<init>(XWPFDocument.java:124)
    at helper.JavaHelper.readTableDataFull(JavaHelper.java:84)
    at helper.JavaHelper.main(JavaHelper.java:138)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

I have those jars:

  • dom4j-1.6.1.jar
  • xmlbeans-2.3.0.jar
  • poi-3.9-20121203.jar
  • poi-scratchpad-3.9-20121203.jar
  • poi-ooxml-3.9-20121203.jar
  • ooxml-schemas-1.0.jar

Looks like it should work to me (I have also tried with newer poi libs - same error).

Any ideas ?

2 Answers 2

8

A quick peek at Grepcode tells that the missing method is supported in v1.1 of ooxml-schemas. Also the v1.0 doesn't seem to contain such a method (it only contains a getTrArray() method).

So you may want to use ooxml-schemas-1.1.jar instead of ooxml-schemas-1.0.jar.

Here's the maven dependency of the version you should use at the very least:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>ooxml-schemas</artifactId>
    <version>1.1</version>
</dependency>
Sign up to request clarification or add additional context in comments.

Comments

6

Quoting from the Apache POI FAQ page

Q: I'm using the poi-ooxml-schemas jar, but my code is failing with "java.lang.NoClassDefFoundError: org/openxmlformats/schemas/something"

To use the new OOXML file formats, POI requires a jar containing the file format XSDs, as compiled by XMLBeans. These XSDs, once compiled into Java classes, live in the org.openxmlformats.schemas namespace.

There are two jar files available, as described in the components overview section. The full jar of all of the schemas is ooxml-schemas-1.1.jar, and it is currently around 15mb. The smaller poi-ooxml-schemas jar is only about 4mb. This latter jar file only contains the typically used parts though.

Many users choose to use the smaller poi-ooxml-schemas jar to save space. However, the poi-ooxml-schemas jar only contains the XSDs and classes that are typically used, as identified by the unit tests. Every so often, you may try to use part of the file format which isn't included in the minimal poi-ooxml-schemas jar. In this case, you should switch to the full ooxml-schemas-1.1.jar. Longer term, you may also wish to submit a new unit test which uses the extra parts of the XSDs, so that a future poi-ooxml-schemas jar will include them.

So, as the FAQ explains, in the short term replace the small poi-ooxml-schemas jar with the larger ooxml-schemas jar. In the long term, submit a junit test to Apache POI that uses the bits of the schema jar you want to use, and they'll then appear in the next small poi-ooxml-schemas jar from then on

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.