Spring JSP loading error: NoClassDefFoundError

Every time I try to start the spring boot application 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 tried running samples: spring-boot-sample-web-jsp and spring-boot-sample-web JSP

The result is the same for these samples. I am running an application using IntelliJ IDEA and do not have IDEA why it does not work.

+9
java spring spring-boot spring-mvc jsp
source share
4 answers

There is an error in IntelliJ, which means that provided that the dependencies are not added to the classpath. Assuming you want to stick with IDEA, you have several options:

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

EDIT: The error has been fixed, and the server will start normally if you select the Include checkboxes with the "Provided" area in the startup configuration, below the pathpath class .

+17
source share

The error report suggested another workaround .

You can use <scope>provided</scope> , as suggested in the Spring documentation, and then go to your project settings in IntelliJ. For the module in question, on the Dependencies tab, you will see that the dependencies associated with spring-boot-starter-tomcat are listed as Provided. Changing them to Compilation should force IntelliJ to add them to the classpath.

This has the advantage of not requiring any changes to your pom.xml and allows you to use the Spring boot integration provided by IntelliJ.

+4
source share

I found the best workaround for this error.

If you start the project directly using the command line, you will lose the debugging function provided by the IDE. You can click the Maven Project tab, find spring-boot:run goal , right-click and select debug XXXX . Using this method, you can get the full debugging function that the IDE provides.

+1
source share

Take a look at an example - it uses Spring Boot 2.1.2

https://github.com/AngloIBS/spring-boot-2-web-jsp

I used IntelliJ IDEA 2017.1.6 to create this little demo.

0
source share

All Articles