25

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
O_k
  • 993
  • 1
  • 8
  • 13

7 Answers7

54

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
O_k
  • 993
  • 1
  • 8
  • 13
  • 11
    In maven, I changed the scope like this: `provided` to `compile` and it worked!!. Thanks for the solution. – jacobcs Aug 21 '17 at 20:57
  • Thank you! Works like a charm <3 – amstegraf Jan 31 '18 at 12:44
  • 3
    Note that the 'provided' scope is there for a reason. You don't want your deployables to contain the servlet APIs or other web application server artifacts - this can potentially lead to all kinds of runtime errors when your application gets deployed. In my opinion it is better to use a Spring Boot Run configuration to fix aforementioned error. – Walco Jul 13 '19 at 19:49
  • @Walco I partially understand you point, could you please post your solution? Maybe could be the right answer for other people. Thanks – Aerox May 29 '20 at 14:59
17

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Techflash
  • 707
  • 7
  • 15
  • 1
    Using this suggestion instead of running the class directly from the project explorer in IntelliJ got rid of my ClassNotFoundException I was seeing when trying to debug in IntelliJ. – jovanchohan Mar 12 '19 at 20:43
16

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
WalidRH
  • 161
  • 1
  • 3
5

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!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jacek Góraj
  • 983
  • 1
  • 10
  • 16
  • Thanks this fixes it for me (spring n00b here). How come it does not auto add it however? - project builds fine without it but will crash with dreaded java.lang.NoClassDefFoundError: javax/servlet/ServletContext – Mr_and_Mrs_D Oct 09 '20 at 16:42
1

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ccg ccc
  • 51
  • 3
1

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

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

Prateek Gupta
  • 2,422
  • 2
  • 16
  • 30