我正在使用嵌入式Tomcat 7.0.40进行单元测试。我的webapp使用Servlet 3.x(x> 0)。
我设法在Tomcat上部署我的webapp。
// create the embedded Tomcat
Tomcat tomcat = new Tomcat();
// (...)
// deploy the WAR in the webApp folder
// (...)
// add the WebApp in Tomcat
Context context = tomcat.addWebapp(tomcat.getHost(), "/myapp", webApp.getAbsolutePath());
// (...)
// start the embedded Tomcat
tomcat.start()
但是,当我启动Tomcat时会抛出异常:
(...)
Caused by: java.lang.NoClassDefFoundError: javax/servlet/ServletContainerInitializer
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2895)
at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1173)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1681)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at org.apache.catalina.startup.ContextConfig.getServletContainerInitializer(ContextConfig.java:1659)
at org.apache.catalina.startup.ContextConfig.processServletContainerInitializers(ContextConfig.java:1569)
at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1277)
at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:878)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:369)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5269)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 7 more
Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletContainerInitializer
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
... 25 more
javax.servlet.ServletContainerInitializer本地化在从3.1版开始的javax.servlet-api JAR中。
您知道如何解决此问题吗?
如何在嵌入式Tomcat的类路径中以编程方式添加此库?
我尝试了几种无法解决的方法......
感谢您的帮助。