我使用maven来管理我的Web项目依赖项。我将apache poi依赖项添加到我的pom文件中。它在编译时不显示错误。但是当它运行时,它会在我的MainApplication()类中抛出RuntimeException。虽然它给出了java.lang.NoClassDefFoundError:org / apache / poi / ss / usermodel / Workbook
我有一个MainApplication类。
public class MainApplication extends Application {
private Set<Class<?>> classes = new HashSet<Class<?>>();
HashSet<Object> singletons = new HashSet<Object>();
public MainApplication() {
try {
ClassPathXmlApplicationContext springContext = new ClassPathXmlApplicationContext("applicationContext.xml");
singletons.add(springContext.getBean("transformService", DataTransformService.class));
}
public Set<Class<?>> getClasses() {
return classes;
}
protected ApplicationContext springContext;
public Set<Object> getSingletons() {
return singletons;
}
}
以下是我添加的内容
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.8-beta3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
<scope>provided</scope>
</dependency>
ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/reportv2]] StandardWrapper.Throwable: java.lang.RuntimeException: Failed to construct public com.osg.application.MainApplication()
at org.jboss.resteasy.core.ConstructorInjectorImpl.construct(ConstructorInjectorImpl.java:144) [:]
at org.jboss.resteasy.spi.ResteasyDeployment.createApplication(ResteasyDeployment.java:243) [:]
at org.jboss.resteasy.spi.ResteasyDeployment.start(ResteasyDeployment.java:191) [:]
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.init(ServletContainerDispatcher.java:67) [:]
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.init(HttpServletDispatcher.java:36) [:]
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1208) [:6.0.0.Final]
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:955) [:6.0.0.Final]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:188) [:6.0.0.Final]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) [:6.0.0.Final]
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:181) [:6.0.0.Final]
at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.event(CatalinaContext.java:285) [:1.1.0.Final]
at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.invoke(CatalinaContext.java:261) [:1.1.0.Final]
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:88) [:6.0.0.Final]
at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:100) [:6.0.0.Final]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) [:6.0.0.Final]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [:6.0.0.Final]
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158) [:6.0.0.Final]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [:6.0.0.Final]
at org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.invoke(ActiveRequestResponseCacheValve.java:53) [:6.0.0.Final]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362) [:6.0.0.Final]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [:6.0.0.Final]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:654) [:6.0.0.Final]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:951) [:6.0.0.Final]
at java.lang.Thread.run(Thread.java:695) [:1.6.0_65]
Caused by: java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Workbook
at java.lang.Class.getDeclaredConstructors0(Native Method) [:1.6.0_65]
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2446) [:1.6.0_65]
at java.lang.Class.getDeclaredConstructors(Class.java:1872) [:1.6.0_65]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:227) [:3.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:930) [:3.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:903) [:3.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485) [:3.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) [:3.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291) [:3.0.5.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) [:3.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) [:3.0.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) [:3.0.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580) [:3.0.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895) [:3.0.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425) [:3.0.5.RELEASE]
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139) [:3.0.5.RELEASE]
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83) [:3.0.5.RELEASE]
at com.osg.application.MainApplication.<init>(MainApplication.java:19) [:]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) [:1.6.0_65]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) [:1.6.0_65]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) [:1.6.0_65]
at java.lang.reflect.Constructor.newInstance(Constructor.java:513) [:1.6.0_65]
at org.jboss.resteasy.core.ConstructorInjectorImpl.construct(ConstructorInjectorImpl.java:132) [:]
... 23 more
答案 0 :(得分:9)
Apache POI provides a components page详细介绍了项目的所有不同部分,您需要的罐子以及您需要的Maven工件。如果你看那里,你会看到以下内容:
|Component | Application type | Maven artifactId | Notes
|Common SS | Excel XLS and XLSX | poi-ooxml | WorkbookFactory and friends all require poi-ooxml, not just core poi |
如上所述,如果您想使用所有常见的org.apache.poi.ss
课程,则需要依赖poi-ooxml
,而不仅仅依赖于poi
其次,编译时间!=运行时间。只是因为一个jar被maven吸了下来并且可以编译,并不意味着它会在你的代码运行时出现。您还需要确保使用代码打包依赖项,或者确保它们在运行时位于类路径中。
您似乎(来自堆栈跟踪)正在编写Web应用程序,因此您需要确保所有依赖项都放入/WEB-INF/lib/
的战争中,以便它们在运行时存在。
最后,POI 3.8 beta 3是一个非常奇怪的版本。你应该选择最新的稳定版(现在是3.9版),或者最新的版本(不是3.10 beta版)。有关当前版本的详细信息,请参阅POI homepage。