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

本文介绍了一种在使用IntelliJ IDEA运行SpringBoot项目结合JSP时遇到的ClassNotFoundException问题,并提供了具体的解决步骤。通过调整tomcat-embed-jasper依赖的scope属性,解决了IDEA环境下无法启动的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近使用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容器启动方式不同导致的吧。在国内未找到此问题的解决方案,特写博客告之,方便后来人~

### Jeecgboot 3.8.0 中 `java.lang.ClassNotFoundException` 错误分析与解决方案 在Jeecgboot 3.8.0项目启动时,如果出现`java.lang.ClassNotFoundException: org.jeecg.JeecgSystemApplication`错误,这通常表明Java运行时环境无法找到指定的类文件。此类问题可能由以下原因引起: #### 1. **依赖项缺失** 如果项目的`pom.xml`文件中缺少必要的依赖项,或者依赖项未正确加载到构建路径中,可能会导致此问题。需要检查是否正确引入了Jeecgboot的核心模块[^1]。 ```xml <dependency> <groupId>org.jeecgframework</groupId> <artifactId>jeecg-boot-starter-core</artifactId> <version>3.8.0</version> </dependency> ``` #### 2. **类路径配置错误** 确保`org.jeecg.JeecgSystemApplication`类位于正确的包路径下,并且已被正确编译到目标目录(如`target/classes`)。如果使用的是IDE(如IntelliJ IDEAEclipse),可以尝试清理并重新构建项目以确保所有类文件被正确生成[^2]。 -Maven项目中,执行以下命令: ```bash mvn clean install ``` - 在IDE中,选择“Rebuild Project”或“Clean and Build”。 #### 3. **Spring Boot 启动类位置不正确** Spring Boot要求`@SpringBootApplication`注解所在的启动类必须位于其他组件扫描路径的根目录或其父目录中。如果`JeecgSystemApplication`类的位置不符合这一规则,可能导致组件扫描失败[^3]。 确保`JeecgSystemApplication`类位于项目的主包路径下,例如: ```java package org.jeecg; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class JeecgSystemApplication { public static void main(String[] args) { SpringApplication.run(JeecgSystemApplication.class, args); } } ``` #### 4. **运行环境问题** 如果项目的运行环境(如JDK版本、Tomcat版本等)与Jeecgboot 3.8.0的要求不匹配,也可能引发此类问题。请确保使用的JDK版本符合官方文档中的要求(通常是JDK 8或更高版本)[^4]。 检查JDK版本: ```bash java -version ``` #### 5. **打包或部署问题** 如果是通过打包后的JAR文件运行项目,可能是由于打包过程中未正确包含依赖项。确保使用以下命令进行打包,并验证生成的JAR文件是否包含`org.jeecg.JeecgSystemApplication`类: ```bash mvn clean package -DskipTests ``` 使用以下命令检查JAR文件内容: ```bash jar tf your-application.jar | grep JeecgSystemApplication ``` --- ### 示例代码:验证类路径 以下代码可用于验证类是否存在于运行时环境中: ```java public class ClassLoaderTest { public static void main(String[] args) { try { Class.forName("org.jeecg.JeecgSystemApplication"); System.out.println("Class found successfully."); } catch (ClassNotFoundException e) { System.err.println("Class not found: " + e.getMessage()); } } } ``` ---
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值