Spring Initializr首个应用运行异常:构建成功但无法启动
Spring Boot项目构建成功但无法启动问题排查
我刚开始学习Spring框架,遇到一个问题:我的Spring Boot项目构建成功,但无法完成启动运行。我已在网上搜索但未找到相关解决方案。以下是项目相关文件及运行错误信息:
pom.xml 文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.2.2</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.sivcodes</groupId> <artifactId>spring-boot-example</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-boot-example</name> <description>Demo project for Spring Boot</description> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
启动类代码(SpringBootExampleApplication.java)
package com.sivcodes; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootExampleApplication { public static void main(String[] args) { SpringApplication.run(SpringBootExampleApplication.class, args); } }
运行错误信息
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled. 2024-02-01T15:38:21.123+05:30 ERROR 1444 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPLICATION FAILED TO START *************************** Description: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that's listening on port 8080 or configure this application to listen on another port.
解决方案
1. 终止占用8080端口的进程
- Windows系统:
- 打开命令提示符,执行:
netstat -ano | findstr :8080,找到对应进程的PID号 - 执行:
taskkill /PID <PID号> /F强制终止该进程
- 打开命令提示符,执行:
- Linux/macOS系统:
- 打开终端,执行:
lsof -i :8080,找到对应进程的PID号 - 执行:
kill -9 <PID号>终止该进程
- 打开终端,执行:
2. 修改项目配置指定新端口
在项目的src/main/resources目录下创建或修改application.properties(或application.yml)文件:
- properties格式:
server.port=8081 - yml格式:
server: port: 8081
3. 启动时通过命令行指定端口
- 使用Maven直接启动:
mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=8081 - 打包成jar包后启动:
java -jar spring-boot-example-0.0.1-SNAPSHOT.jar --server.port=8081
内容的提问来源于stack exchange,提问作者siv.ncapayi
相关产品推荐
相关产品推荐

