本文介绍了Java-NoClassDefFoundError XSSFWorkbook的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了到XLSX应用程序的CSV-

解决方案

这是一个非常常见的问题.项目的依赖项未包含在您执行的jar中,从而导致 NoClassDefFoundError .

要解决此问题,请将其添加到 pom.xml plugins 部分:

 < plugin>< groupId> org.apache.maven.plugins</groupId>< artifactId> maven-assembly-plugin</artifactId><执行><执行>< phase>包装</phase><目标>< goal>单</goal></goals><配置>< archive><清单>< mainClass>com.mypackage.Main</mainClass></manifest></archive>< descriptorRefs>< descriptorRef>具有依赖关系的</descriptorRef></descriptorRefs></configuration></execution></executions></plugin> 

然后执行 mvn clean package ,在目标目录中完成后,您将找到包含所有必需依赖项的可执行文件 jar_name-jar-with-dependencies.jar ./p>

最后简单地运行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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 12:58