O_k
O_k

Reputation: 1013

Spring Boot - java.lang.ClassNotFoundException: javax.servlet.ServletContext and Unable to start EmbeddedWebApplicationContext

I have a Spring Boot application (Grails 3.1.8), but when I run the application in IntelliJ IDEA I get the following error:

Caused by: java.lang.IllegalStateException: Failed to introspect annotated methods on class org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport
    at org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:163)
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:292)
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:232)
    at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:510)
    ... 26 common frames omitted
Caused by: java.lang.NoClassDefFoundError: javax/servlet/ServletContext
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.getDeclaredMethods(Class.java:1975)
    at org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:152)
    ... 29 common frames omitted
Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletContext
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 33 common frames omitted

I tried adding javax-servlet-api dependency as suggested in an answer to Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: javax/servlet/ServletContext

But then I started getting:


ERROR org.springframework.boot.SpringApplication - Application startup failed
org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766)
    at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
    at grails.boot.GrailsApp.run(GrailsApp.groovy:55)
    at grails.boot.GrailsApp.run(GrailsApp.groovy:374)
    at grails.boot.GrailsApp.run(GrailsApp.groovy:363)
    at grails.boot.GrailsApp$run.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133)
    at closemytab.Application.main(Application.groovy:13)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:185)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:158)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130)
    ... 18 common frames omitted

This post then suggests to remove the javax-servlet-api: Spring boot -- Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean

But then I face the original issue.

Any thoughts?

Upvotes: 25

Views: 62149

Answers (8)

phancuongviet
phancuongviet

Reputation: 435

I resolved the problem replace

implementation 'javax.servlet:jstl' --> implementation group: 'jakarta.servlet.jsp.jstl', name: 'jakarta.servlet.jsp.jstl-api', version: '3.0.0'

Upvotes: 0

Prateek Gupta
Prateek Gupta

Reputation: 2770

I had the same issue for the maven project and what I did was

  1. Select Modify Options

enter image description here

  1. Select the option Add dependencies with "provided" scope to classpath

enter image description here

  1. Apply the changes

enter image description here

Upvotes: 1

Jacek Góraj
Jacek Góraj

Reputation: 1023

Adding the dependency below fixed my issue:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Remember to have a parent dependency as well!

Upvotes: 5

Jarnail Singh
Jarnail Singh

Reputation: 11

Exception is java.lang.NoClassDefFoundError: javax/servlet/ServletContext only means you are not providing the Tomcat dependency to your application. In case you are using IntelliJ IDEA, it’s really simple to do that as shown below.

Select your project → open the Maven panel (it mostly resides on the top right side of the panel) → select embedded.

Click here for a screenshot

Upvotes: 1

O_k
O_k

Reputation: 1013

The issue was with the build.gradle file:

provided "org.springframework.boot:spring-boot-starter-tomcat"

IntelliJ IDEA wasn't happy with the provided.

As soon as I switched to

compile "org.springframework.boot:spring-boot-starter-tomcat"

the application worked.

Upvotes: 56

Techflash
Techflash

Reputation: 767

Run with a Maven Spring Boot goal:

spring-boot:run

Steps to set up Maven configuration in IntelliJ IDEA:

Menu Debug/Run Configuration → Click on the + button visible at the top left → Select Maven → Set command line to spring-boot:run.

Upvotes: 19

ccg ccc
ccg ccc

Reputation: 51

For me, it's because of the Tomcat version. I changed Tomcat version from 7.5 to 8.5, and the problem's solved.

Upvotes: 1

WalidRH
WalidRH

Reputation: 171

If you are using IntelliJ IDEA, you need to enable ' Include dependencies with "Provider" scope in menu RunEdit ConfigurationsApplication → 'your main class' (by default it's disabled)

Enter image description here

Upvotes: 17

Related Questions