java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Row [英] java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Row
问题描述
我制作了一个从 excel(xls 文件)读取并将内容显示到 JTable 的小型应用程序.在 Eclipse 中一切正常,但是当我创建 jar 文件并尝试运行它时,出现以下问题:
java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Row
我发现奇怪的是问题出在行上,当 Workbook 和 Sheet 在行之前被调用并且没有出现问题时(至少从我所见).
我研究了很多,似乎主要是 jar 文件不在类路径中,但是打开 jar 和清单文件我可以看到所有 jar 都存在.
Class-Path: poi-ooxml-4.0.1.jar poi-4.0.1.jar commons-codec-1.11.jar commons-collections4-4.2.jar commons-math3-3.6.1.jar commons-compress-1.18.jar 曲线api-1.05.jar poi-ooxml-schemas-4.0.1.jar xmlbeans-3.0.2.jar
这是我在 pom.xml 文件中的内容:
<插件><插件><!-- 构建一个可执行的 JAR --><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><配置><存档><清单><addClasspath>true</addClasspath><classpathPrefix>./</classpathPrefix><mainClass>com.clientdb.classes.DynamicRegForm</mainClass></清单></存档></配置></插件></plugins></build><依赖项><依赖><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.0.1</version></依赖><依赖><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.0.1</version></依赖><依赖><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>4.0.1</version></依赖></依赖项>
我也试过下载 jar 文件并将它们添加到项目中,而不是将依赖项添加到 pom 文件,但仍然出现相同的错误.有什么想法吗?
可能只有在运行 jar 时才会得到这个因为依赖项不可用/打包在其中..>
尝试生成一个fat jar"(也称为uber-jar),它会将您的所有依赖项打包在该jar 中:
<插件><插件><groupId>org.apache.maven.plugins</groupId><artifactId>maven-shade-plugin</artifactId><执行><执行><phase>包</phase><目标><目标>阴影</目标></目标></执行></执行><配置><finalName>YOUR_JAR_FINAL_NAME</finalName></配置></插件></plugins></build>
与maven-shade-plugin
相关的文档可以是在这里找到
更新:由于您使用的是可运行的 jar 文件,您可以按照 文档的这一部分与可执行文件
相关I've made a small application that reads from an excel (xls file) and displays the contents to a JTable. Everything is working fine in eclipse, yet when I create the jar file and try to run it, I get the following issue:
java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Row
The weird thing I find is that the issue is with the Row, when Workbook and Sheet is called before the row and gives no trouble (at least from what I can see).
I've researched alot and it mainly seems to be with the jar files not being in the Class-Path, but opening the jar and the manifest file I can see all the jars are present.
Class-Path: poi-ooxml-4.0.1.jar poi-4.0.1.jar commons-codec-1.11.jar commons-collections4-4.2.jar commons-math3-3.6.1.jar commons-compress-1.18.jar curvesapi-1.05.jar poi-ooxml-schemas-4.0.1.jar xmlbeans-3.0.2.jar
This is what I have in my pom.xml file:
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>./</classpathPrefix>
<mainClass>com.clientdb.classes.DynamicRegForm</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.0.1</version>
</dependency>
</dependencies>
I have also tried downloading the jar files and adding them to the project instead of adding the dependency to pom file, and still the same error. Any ideas?
Probably you are getting this only when you are running your jar because the dependencies are not available/packaged inside of it.
Try generating a "fat jar" (also known as uber-jar), it will package all your dependencies inside the jar:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<finalName>YOUR_JAR_FINAL_NAME</finalName>
</configuration>
</plugin>
</plugins>
</build>
Documentation related to the maven-shade-plugin
can be found in here
UPDATE: Since you are using a runnable jar file, you can follow this section of the documentation related to Executable Jars
这篇关于java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Row的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!