Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError问题解决
场景复现
采用Maven构建非Spring项目,在开发阶段能够正常开发(采用IDEA直接run,是可以运行的),通过maven打包后,通过java -jar 运行项目报错:Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError, 查看网上各种办法不得行,后面捣鼓半天明白原来是在打包时候出现的问题。
问题描述
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Workbook
at gui.MainGUI.<init>(MainGUI.java:19)
at Application$1.run(Application.java:15)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.Workbook
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
问题原因
在某些第三发包的引入下,采用以下maven构建时会将第三方jar忽略掉,也就是没有进行打包,导致项目无法运行
原来打包的maven文件为:
...
...
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>Application</mainClass>
<!-- 此处为主入口 -->
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
解决方式
采用将依赖jar打包到项目中,修改maven文件
pom.xml
...
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<archive>
<manifest>
<mainClass>Application</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<!-- 添加此项后,可直接使用mvn package | mvn install -->
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
标题:Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError问题解决
作者:sirwsl
地址:https://www.wslhome.top/articles/2021/12/30/1640845393148.html