java.lang.NoClassDefFoundError: org.apache.poi.POIXMLDocument troubleshooting and resolution

The project was packaged and deployed, and the jar package was used to run it in the deployment environment. When using poi-related services, an error occurred:
java.lang.NoClassDefFoundError: org.apache.poi.POIXMLDocument
Insert image description here
. Then I looked for some solutions and sorted out the processing methods:

  1. Using XSSWorkbook, an error occurred while using
  2. pom files introduce fewer dependencies
  3. Package dependency conflict
  4. Using modules without declaring required dependencies (this is the problem I encountered)
  5. Modify the code in the POIXMLDocumentPart file yourself
Solution to the first situation:

The biggest possibility is that the introduced jar package is too high or too low.
There are three poi related poi: poi, poi-ooxml, poi-ooxml-schemas

The best version to introduce is version 3.15

  • Too low, no method can be found , resulting in an error
  • Too high, because the poi package series is updated quickly and the updated version is not stable enough, causing many problems

Of course, if you have been using a higher version, there is no need to forcibly downgrade the version. The problem may not be the first one, because version 3.15 also hides some problems.

Solution to the second situation

When such an exception is thrown when operating word or excel in java, it is usually because the pom file introduces less dependencies.

Add the maven dependencies in the corresponding pom

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

Generally, this situation is rare and will be added, but it is not an exception and is an ignored problem.

Solution to the third situation

When introducing dependencies in the pom file, other versions of jar packages are introduced, causing the corresponding class to not be found during runtime.

Globally query related pom dependencies and remove redundant dependency files

Solution to the fourth situation

The current module A does not introduce the required dependencies, and the dependencies are introduced into the main module and the public module B. As a result, the A module cannot determine which class to use at runtime, which is similar to repeated dependencies.

Find the class file where the error occurred. The class file where I made the error was FileStencilHandleUtils.
Insert image description here
Then I found the pom in this class module, added the dependencies, then looked up the pom, found the public pom and main pom files, and removed the dependencies inside.

This will solve the problem. Of course, if there are other modules that need to be used, you can keep the main pom and declare the required dependencies for the required modules.

Solution to the fifth situation

It has been shown above that the poi series has many bugs. You can modify the specific methods to fix the bugs. However, this method is not suitable for everyone, so use it with caution.

Download or find the corresponding jar package. Unzip
Insert image description here
the jar package and find the file XWPFStylesDocument.java. The file location is \org.apache.poi.xwpf.converter.core-1.0.6-sources\org\apache\poi\xwpf\ converter\core\styles

Open the file and modify the methods in the file

After the modification is completed, put the entire module into the code, or re-package it into a jar package and import the dependencies.

The fifth solution comes from the original article of the blogger: "The Daily Log of the Great Demon King".
Original link: https://blog.csdn.net/TaoShao521/article/details/126170434

————————————————

Additional knowledge points:
The NoClassDefFoundError problem is different from the ClassNotFoundException problem. The former usually occurs when the corresponding class can be found during compilation, but cannot be found at runtime. The latter occurs when the corresponding class cannot be found during compilation.

おすすめ

転載: blog.csdn.net/weixin_45937536/article/details/128904930