Java - NoClassDefFoundError XSSFWorkbook [英] Java - NoClassDefFoundError XSSFWorkbook
问题描述
我创建 CSV 到 XLSX 应用程序 -
这是一个很常见的问题.您项目的依赖项未包含在您执行的 jar 中,导致 NoClassDefFoundError
.
要修复将其添加到 pom.xml
的 plugins
部分:
<groupId>org.apache.maven.plugins</groupId><artifactId>maven-assembly-plugin</artifactId><执行><执行><phase>包</phase><目标><目标>单</目标></目标><配置><存档><清单><mainClass>com.mypackage.Main</mainClass></清单></存档><descriptorRef>jar-with-dependencies</descriptorRef></descriptorRefs></配置></执行></执行></插件>
然后执行 mvn clean package
,在目标目录中完成后,您将找到一个可执行文件 jar_name-jar-with-dependencies.jar
,其中包含所有必需的依赖项.
最后简单地run java -jar jar_name-jar-with-dependencies.jar
.
I creating CSV to XLSX app - My first question
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/xssf/usermodel/XSSFWorkbook
at com.test.csv2xlsx.Csv2Xlsx.<init>(Csv2Xlsx.java:35)
at com.test.csv2xlsx.Csv2Xlsx.main(Csv2Xlsx.java:49)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.xssf.usermodel.XSSFWorkbook
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 2 more
I read all the articles about this error, but I already had all dependencies
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.3</version>
</dependency>
</dependencies>
Now I am not sure where is the problem
This is a pretty common issue. Your project's dependencies are not included in the jar you execute, leading to NoClassDefFoundError
.
To fix add this to the plugins
section of your pom.xml
:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>
com.mypackage.Main
</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
Then execute mvn clean package
and when finished inside target directory you will find an executable jar_name-jar-with-dependencies.jar
which contains all the required dependencies.
Finally simply run java -jar jar_name-jar-with-dependencies.jar
.
这篇关于Java - NoClassDefFoundError XSSFWorkbook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!