Getting java.lang.ClassNotFoundException: javax.servlet.ServletContext in JUnit
You have a single xml file for your ApplicationContext
in this file there is a <mvc:annotation-driven />
tag. This tag loads different web related resources (view resolvers, handler mappings etc.) and as such requires the servlet api to be available.
You already should have the servlet api on your classpath as a provided dependency in maven.
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
Next to that you might want to remove the <mvc:annotation-driven />
tag and put it in a seperate configuration file. This is also a tag which should (in general speaking) be loaded by the DispatcherServlet
. (I assume here the applicationContext.xml is, as default, loaded by the ContextLoaderListener
).
Spring boot - java.lang.ClassNotFoundException: javax.servlet.ServletContext and Unable to start EmbeddedWebApplicationContext
The issue was with the build.gradle
provided "org.springframework.boot:spring-boot-starter-tomcat"
Intellij wasn't happy with the provided
as soon as I switched to
compile "org.springframework.boot:spring-boot-starter-tomcat"
the application worked
java.lang.NoClassDefFoundError: javax/servlet/ServletContext
OK here is what I had to do to solve this specific issue:
- IntelliJ needed to have a definition for Gradle Task called bootRun within the Debug/Run Configuration. This allows me to now run back-end code or debug back-end code updates in IntelliJ.
- Next I had to restructure my IntelliJ directories because IntelliJ 15 had installed both a parent and a child JRE folder which is incorrect so therefore IntelliJ displayed and exception stating that it could not find the a) java.exec nor the b) jvm.cfg files under it's JRE/Lib sub-directory.
Once I fixed these two issues both builds and debugging seemed to work for Java Spring back end. I followed up with IntelliJ about the quirky JRE installation.
Error when trying out SPRING (java.lang.NoClassDefFoundError: javax/servlet/ServletContext)
You must use the same version for all Spring dependencies:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.3</version>
</dependency>
Please update at least all components to version 5.3.3 or to the latest available version.
Spring is a project with multiple modules: https://github.com/spring-projects/spring-framework and different versions of Spring components can throw many different exceptions like: java.lang.NoSuchMethodError
, java.lang.NoClassDefFoundError
and etc.
Java Spring MVC - java.lang.NoClassDefFoundError: javax/servlet/ServletContext
You need to use @WebAppConfiguration
annotation in your test class. Here is what the documentation states:
@WebAppConfiguration A class-level annotation that is used to declare
that the ApplicationContext loaded for an integration test should be a
WebApplicationContext. The mere presence of @WebAppConfiguration on a
test class ensures that a WebApplicationContext will be loaded for the
test, using the default value of "file:src/main/webapp" for the path
to the root of the web application (i.e., the resource base path). The
resource base path is used behind the scenes to create a
MockServletContext which serves as the ServletContext for the test’s
WebApplicationContext.
This works with provided
scope of javax.servlet-api
.
Besides, you can't use @EnableWebMvc
in the configuration class you use for testing. Best if you create a separate configuration class that will be only used for testing, without @EnableWebMvc
. You can check out this discussion: java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling
Spring @ControllerAdvice - NoClassDefFoundError: javax/servlet/ServletException
It doesn't require for a standalone jar application: providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
. Starter-web includes one
When you've declared this dependency you said you build to exclude tomcat for a standalone run. This means - no servlet library inside
If you deploy this app somewhere, then just exclude tomcat from starter-web
, like this:
compile('org.springframework.boot:spring-boot-starter-web'){
exclude module: "spring-boot-starter-tomcat"
}
Related Topics
Spring - Read Property Value from Properties File in Static Field of Class
Spring-Data-Jpa Repository - Underscore on Entity Column Name
Log File Is Created But File Is Empty
Delete Everything After Part of a String
How to Append a Query Parameter to an Existing Url
Java.Sql.Sqlexception: Field Doesn't Have a Default Value
Simplifying the If-Else Condition Using Java Functional Style Programming
Keytool Error Bash: Keytool: Command Not Found
Scheduling a Job With Spring Programmatically (With Fixedrate Set Dynamically)
Typecasting an Object from Parent Class to Child
How to Stop the Items Duplication in Recyclerview Android
Reading Data from Nested Json Object Using Java
Error: Could Not Find or Load Main Class in Intellij Ide
How to Use a Regex to Search Backwards Effectively
Multiple Queries Executed in Java in Single Statement
How to Return a Custom Object from a Spring Data JPA Group by Query