Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot JSP error: NoClassDefFoundError

Everytime I try to run spring boot app configured with JSP I get this error:

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)
.............
[more errors/exceptions]
.............
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)

I've tried to run samples: spring-boot-sample-web-jsp and spring-boot-sample-web-jsp

The result is the same for those samples. I run application using IntelliJ IDEA and have no IDEA why it does not work.

like image 699
user3541916 Avatar asked Sep 11 '15 20:09

user3541916


2 Answers

There's a bug in IntelliJ that means that provided dependencies aren't added to the classpath. Assuming you want to stick with IDEA, you have a few options:

  • Manually configure the classpath in IDEA
  • Run the samples on the command line using mvn spring-boot:run
  • Remove all occurrences of <scope>provided</scope> from the pom. This will mean that app can't be deployed as a war to Tomcat or similar

EDIT: The bug is fixed and the server will start normally, as long as you tick the Include dependencies with "Provided" scope checkbox in the run configuration, below classpath.

like image 89
Andy Wilkinson Avatar answered Oct 09 '22 01:10

Andy Wilkinson


I found a better workaround for this bug.

If you run your project using command line directly, you will lose the debug function provided by IDE. You can click the Maven Project tab, find the spring-boot:run goal, right click then select debug XXXX. By using this way you can get full debug feature that your IDE gives.

like image 32
Neo Avatar answered Oct 09 '22 00:10

Neo