Eclipse启动项目成功,IDEA报错java.lang.ClassNotFoundException: javax.servlet.Filter

来源:互联网 发布:优学派软件下载 编辑:程序博客网 时间:2024/05/29 18:08

最近使用Spring Boot开发web项目时,前端使用jsp页面。前期在公司使用eclipse开发,运行都很正常的。在家使用IDEA运行死活不行,报错java.lang.ClassNotFoundException: javax.servlet.Filter。

前端使用jsp,虽然spring boot是不建议的,但是对jsp是最熟悉的,所以还是使用。maven的pom.xml文件需要配置如下(使用tomcat作为web容器,spring boot的依赖省略):

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

在eclipse上运行main方法,项目是启动成功的。但是在IDEA上运行main方法之后报错:

Caused by: java.lang.ClassNotFoundException: javax.servlet.Filter    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[?:1.8.0_131]    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[?:1.8.0_131]    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) ~[?:1.8.0_131]    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[?:1.8.0_131]    at org.springframework.boot.devtools.restart.classloader.RestartClassLoader.loadClass(RestartClassLoader.java:151) ~[spring-boot-devtools-1.5.3.RELEASE.jar:1.5.3.RELEASE]    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[?:1.8.0_131]    at org.springframework.context.annotation.ConfigurationClassParser.asSourceClass(ConfigurationClassParser.java:687) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]    at org.springframework.context.annotation.ConfigurationClassParser$SourceClass.getInterfaces(ConfigurationClassParser.java:870) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]    at org.springframework.context.annotation.ConfigurationClassParser.processInterfaces(ConfigurationClassParser.java:363) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:320) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:245) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:190) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:292) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:245) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:198) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:167) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]    ... 17 more

查看maven的依赖,发现已经有这个类的存在,但是为什么还会报出找不到该类呢?!
javax.servlet.Filter类

在排除了Jar包冲突之后,在stackoverflow上同样看到有人提问类似问题,也看到了有人回复答案,stackoverflow链接1 stackoverflow链接2。在自己的实践下果然成功了。解决办法如下:

    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-tomcat</artifactId>    </dependency>    <dependency>        <groupId>org.apache.tomcat.embed</groupId>        <artifactId>tomcat-embed-jasper</artifactId>    </dependency>
 <scope>provided</scope> <!-- 这行删除或者provided换成compile(scope的默认值) -->

问题到此解决,究其为什么eclipse可以运行,IDEA不能运行的原因,应该是两个IDE在内嵌tomcat容器启动方式不同导致的吧。在国内未找到此问题的解决方案,特写博客告之,方便后来人~

阅读全文
1 0
原创粉丝点击