Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NoClassDefFoundError: javax/servlet/ServletContext

I just upgraded to IntelliJ 15.0 from 14.1 (I was in a hurry and enter image description here forgot to save the previous plug state for 14.1) and I'm attempting to configure general run/debug configuration settings with Spring Boot in Intellij using Gradle. In the configuration tab I have added 1) the Main class, 2) JRE, and 3) class path of module in IntelliJ. I use Spring Boot as the selected configuration. However, when select Run I get this error:

2015-11-07 22:00:21.457 ERROR 10632 --- [main] 
o.s.boot.SpringApplication : Application startup failed
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:140)
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:289) 
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:229) 
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:196)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:165)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:306)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:239)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:254)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:94)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:94)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:606)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:462)
at    org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at org.awana.mozo.club.Application.main(Application.java:37)
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: 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)
2015-11-07 22:00:21.457  INFO 10632 --- [           main]    s.c.a.AnnotationConfigApplicationContext : Closing   org.springframework.context.annotation.AnnotationConfigApplicationContext@3788f3    : startup date [Sat Nov 07 22:00:21 CST 2015]; root of context hierarchy
2015-11-07 22:00:21.458  WARN 10632 --- [           main]   s.c.a.AnnotationConfigApplicationContext : Exception thrown from    ApplicationListener handling ContextClosedEvent
java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: org.springframework.context.annotation.AnnotationConfigApplicationContext@3788f3: startup date [Sat Nov 07 22:00:21 CST 2015]; root of context hierarchy
at  org.springframework.context.support.AbstractApplicationContext.getApplicationEventMulticaster(AbstractApplicationContext.java:344)
at   org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:331)
at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:869)
at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:836)
at  org.springframework.boot.SpringApplication.run(SpringApplication.java:342)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at org.awana.mozo.club.Application.main(Application.java:37)
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)
2015-11-07 22:00:21.459  WARN 10632 --- [           main]  s.c.a.AnnotationConfigApplicationContext : Exception thrown from LifecycleProcessor on context close
java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' before invoking lifecycle methods via the context:  org.springframework.context.annotation.AnnotationConfigApplicationContext@3788f3: startup date [Sat Nov 07 22:00:21 CST 2015]; root of context hierarchy
at   org.springframework.context.support.AbstractApplicationContext.getLifecycleProcessor(AbstractApplicationContext.java:357)
at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:877)
at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:836)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:342)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at  org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at org.awana.mozo.club.Application.main(Application.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at  sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at  sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.jav    a:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Exception in thread "main" 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(Sta    ndardAnnotationMetadata.java:140)
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigu    rationClass(ConfigurationClassParser.java:289)
at org.springframework.context.annotation.ConfigurationClassParser.processConfigura    tionClass(ConfigurationClassParser.java:229)
at org.springframework.context.annotation.ConfigurationClassParser.parse(Configurat    ionClassParser.java:196)
at org.springframework.context.annotation.ConfigurationClassParser.parse(Configurat    ionClassParser.java:165)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processCo    nfigBeanDefinitions(ConfigurationClassPostProcessor.java:306)            
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProce    ssBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:239)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBean    DefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:254)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBean    FactoryPostProcessors(PostProcessorRegistrationDelegate.java:94)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactory    PostProcessors(AbstractApplicationContext.java:606)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:462)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at org.awana.mozo.club.Application.main(Application.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.jav    a:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 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)
Process finished with exit code 1
like image 441
user1548875 Avatar asked Nov 08 '15 05:11

user1548875


4 Answers

Had same problem. Seemed specific to trying to run a spring boot web app via IntelliJ 15.x spring boot run configuration. Since I was using maven I instead created a maven run configuration specifying the command line action as spring-boot:run - this works both in run and debug modes. Screenshot attached.

enter image description here

like image 179
arcseldon Avatar answered Oct 18 '22 23:10

arcseldon


Had the same problem, fixed it by fixing pom.xml changed in pom.xml:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

changing it to:

        <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>compile</scope>
    </dependency>

this solved the problem :)

like image 30
MichaelK Avatar answered Oct 19 '22 00:10

MichaelK


OK here is what I had to do to solve this specific issue:

  1. 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.
  1. 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.

like image 31
user1548875 Avatar answered Oct 18 '22 23:10

user1548875


Had the same issue when using Community Edition that has no springboot template.

The issue can be solved in Edit Configurations... -> Modify options -> Check Java: Include dependencies with "Provided" scope

like image 21
Jesper Knudsen Avatar answered Oct 19 '22 00:10

Jesper Knudsen