构造函数抛出异常;嵌套异常是 java.lang.NoClassDefFoundError: javax/servlet/ServletContext

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24393684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:57:04  来源:igfitidea点击:

Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: javax/servlet/ServletContext

javaspringjunit

提问by Jaxox

I am trying to setup the Junit test with the MockMVC.

我正在尝试使用 MockMVC 设置 Junit 测试。

From this link- "either must not use the Servlet API or you need to provide it on the classpath".

从这个链接- “要么不能使用 Servlet API,要么你需要在类路径上提供它”。

I added the following to pom.xml but didn't work. any idea?

我将以下内容添加到 pom.xml 但没有用。任何的想法?

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>

here is the trace

这是痕迹

org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.test.context.web.WebDelegatingSmartContextLoader]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: javax/servlet/ServletContext
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:163)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:105)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:130)
    at org.springframework.test.context.ContextLoaderUtils.resolveContextLoader(ContextLoaderUtils.java:118)
    at org.springframework.test.context.ContextLoaderUtils.buildMergedContextConfiguration(ContextLoaderUtils.java:594)
    at org.springframework.test.context.ContextLoaderUtils.buildMergedContextConfiguration(ContextLoaderUtils.java:560)
    at org.springframework.test.context.TestContext.<init>(TestContext.java:99)
    at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:117)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:119)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:108)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:31)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:24)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
Caused by: java.lang.NoClassDefFoundError: javax/servlet/ServletContext
    at org.springframework.test.context.web.WebDelegatingSmartContextLoader.<init>(WebDelegatingSmartContextLoader.java:36)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:148)
    ... 22 more
Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletContext
    at java.net.URLClassLoader.run(URLClassLoader.java:366)
    at java.net.URLClassLoader.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 28 more`enter code here`

Ok, Updated based on the comments, so I changed the scope to compile then it seems pass the issue of class not found!

好的,根据评论更新,所以我改变了编译范围,然后似乎通过了找不到类的问题!

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>compile</scope>
    </dependency>

回答by Aniket Thakur

Class is not found at runtime but is available at compile time. You need to add the corresponding jar so that it can be found at runtime. I generally use Ivy and in Eclipse I do

在运行时找不到类,但在编译时可用。您需要添加相应的jar,以便在运行时可以找到它。我通常使用 Ivy,在 Eclipse 中我使用

Project -> Properties ->Deployment Assembly -> Add -> java Build Path Enteries -> Ivy -> Finish

There must be something similar for Maven as well.

Maven 也必须有类似的东西。

Also you need javax.servlet-apionly during compile time as the container you are using to run the server will provide the actual APIs at runtime.

此外,您javax.servlet-api只需要在编译期间,因为用于运行服务器的容器将在运行时提供实际的 API。

回答by Serge Ballesta

It is a common issue. As Aniket Thakur said, the container will provide all Java servlet classes at runtime. But during the tests you needa jar to provide them.

这是一个常见的问题。正如 Aniket Thakur 所说,容器将在运行时提供所有 Java servlet 类。但是在测试期间,您需要一个 jar 来提供它们。

The dependency you added to your pom is only the API : it declares everything but contains no implementation. So it will not help. Anyway, you declare it as "provided" which says to maven "don't worry, I know it will be on classpath".

您添加到 pom 的依赖项只是 API:它声明了所有内容但不包含任何实现。所以它不会有帮助。无论如何,您将其声明为“提供”,这对 Maven 说“别担心,我知道它会在类路径上”。

You have to add a dependency that bring the implementation of all Java EE classes in testscope. In my projects I use glassfish even if I later use tomcat as a servlet container, but I once found the dependency googling for the same problem :

您必须添加一个依赖项,将所有 Java EE 类的实现纳入test范围。在我的项目中,即使我后来使用 tomcat 作为 servlet 容器,我也使用 glassfish,但我曾经发现依赖谷歌搜索同样的问题:

<dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.servlet</artifactId>
    <version>3.0</version>
    <scope>test</scope>
</dependency>

It should solve your NoClassDefFoundErrorproblem.

它应该可以解决您的NoClassDefFoundError问题。