10

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.

Michele Lacorte
  • 5,323
  • 7
  • 32
  • 54
user3541916
  • 183
  • 2
  • 10
  • http://stackoverflow.com/questions/19690267/getting-java-lang-classnotfoundexception-javax-servlet-servletcontext-in-junit – Reimeus Sep 11 '15 at 20:13
  • The error is telling you that no class definition was found, and the stack trace for that. Did you track it? – al'ein Sep 11 '15 at 20:14

3 Answers3

20

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.

Salvioner
  • 303
  • 5
  • 16
Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
4

Another workaround was suggested on the bug report.

You can use the <scope>provided</scope> as suggested in the Spring documentation, and then go into your Project Settings in IntelliJ. For the module in question, in the Dependencies tab, you should see that the dependencies related to spring-boot-starter-tomcat are all listed as "Provided". Changing them to "Compile" should force IntelliJ to add them to the classpath.

It has the advantage of both not requiring any modifications to your pom.xml and allowing you to use the Spring Boot integration provided by IntelliJ.

  • 1
    Instead of posting a link, which can go stale, try to include some details about the workaround here, and use the link as a reference! – AP. May 24 '17 at 14:51
  • tomcat-embed-websocket also had to be changed to compile in my case. Thanks a lot – vigamage Aug 05 '17 at 15:03
1

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.

Neo
  • 2,196
  • 5
  • 32
  • 58