Spring boot JSP error: NoClassDefFoundError

前端 未结 3 1554
迷失自我
迷失自我 2021-02-08 13:30

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.         


        
相关标签:
3条回答
  • 2021-02-08 13:46

    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.

    0 讨论(0)
  • 2021-02-08 13:55

    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.

    0 讨论(0)
  • 2021-02-08 14:02

    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.

    0 讨论(0)
提交回复
热议问题