Spring Boot项目启动后直接退出,Process finished with exit code 0求助
Spring Boot项目启动后立即退出(Process finished with exit code 0)问题解决
问题现象
项目启动日志显示应用已完成初始化,但随即进程正常退出,无报错信息,控制台最终输出Process finished with exit code 0。
核心原因分析
从提供的日志来看,应用启动流程中未出现嵌入式Tomcat的启动记录(如Tomcat initialized with port(s): 8080 (http)),说明Web环境未被正确激活,Spring Boot在完成上下文加载后直接正常退出。
解决步骤
1. 检查主启动类配置
确保主类上添加了@SpringBootApplication注解,且main方法正确调用启动逻辑:
package com.example.Crudnew; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class CrudnewApplication { public static void main(String[] args) { SpringApplication.run(CrudnewApplication.class, args); } }
@SpringBootApplication包含自动配置、组件扫描等核心功能,缺失会导致应用无法正确初始化Web环境。
2. 检查配置文件的Web环境设置
查看src/main/resources下的application.properties或application.yml,确认是否存在禁用Web环境的配置:
spring.main.web-application-type=none
如果有,删除该行或修改为:
spring.main.web-application-type=servlet
none类型会让Spring Boot以非Web应用模式启动,完成初始化后直接退出。
3. 验证Web依赖完整性
确认spring-boot-starter-web依赖未丢失关键组件:
打开IntelliJ右侧的Maven工具窗口,展开Dependencies,检查是否存在tomcat-embed-core、tomcat-embed-webapp等Tomcat相关依赖。若缺失,执行以下操作:
- 点击Maven窗口的
Reload All Maven Projects按钮重新导入依赖 - 或在终端执行命令:
mvn clean install -U
4. 强制指定Web应用类型(临时方案)
若以上步骤无效,可在main方法中强制指定Web应用类型:
package com.example.Crudnew; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.WebApplicationType; @SpringBootApplication public class CrudnewApplication { public static void main(String[] args) { SpringApplication app = new SpringApplication(CrudnewApplication.class); app.setWebApplicationType(WebApplicationType.SERVLET); app.run(args); } }
内容的提问来源于stack exchange,提问作者nanda kumar
相关产品推荐
相关产品推荐

