AWS EC2实例部署SpringBoot启动嵌入式Tomcat失败求助
Hey there, let's work through this Tomcat startup issue together. Based on the error message you shared and your pom.xml, here are the key areas to check and fix:
1. Check for Port Conflicts
The most common reason for this error is that the default Tomcat port (8080) is already in use on your EC2 instance. To verify:
- Run this command to check which process is using port 8080:
sudo netstat -tulpn | grep 8080 # Or use ss if netstat isn't installed: sudo ss -tulpn | grep 8080 - If you see a process using the port, you can either kill it (replace
<PID>with the process ID):sudo kill -9 <PID> - Or change the port your Spring Boot app uses by adding this line to your
application.properties(orapplication.yml) file before repackaging the jar:
Don't forget to allow this new port in your EC2 security group (add an inbound rule for TCP traffic on the port).server.port=8081 # Or any other unused port
2. Fix Dependency Conflicts in Your POM.xml
Looking at your pom, there are a few dependencies that are either redundant or version-locked, which can cause classpath conflicts:
- Redundant Tomcat Dependency: The
spring-boot-starter-webalready includes the embedded Tomcat dependency, so you can remove this line entirely:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> - Unnecessary Version Specifiers: Spring Boot's parent POM manages versions for most core dependencies (like Hibernate and Jackson). Remove the hardcoded versions from these dependencies to let Spring Boot handle compatibility:
<!-- Remove the version tag from hibernate-core --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> </dependency> <!-- Remove the version tag from jackson-databind --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> - Redundant Spring Dependencies: Dependencies like
spring-aop,spring-context, andspring-context-supportare already included by other starters (likespring-boot-starter-weborspring-boot-starter-data-jpa). Removing these will reduce the chance of conflicts.
After making these changes, repackage your jar with mvn clean package and re-upload it to S3, then download it again to your EC2 instance.
3. Verify Database Configuration
Your app uses Spring Data JPA and MySQL, so a misconfigured database connection can prevent Tomcat from starting. Here's what to check:
- Ensure your
application.propertieshas the correct database credentials and URL:spring.datasource.url=jdbc:mysql://<your-db-host>:3306/<db-name>?useSSL=false&serverTimezone=UTC spring.datasource.username=<db-username> spring.datasource.password=<db-password> spring.jpa.hibernate.ddl-auto=update - If you're using AWS RDS, confirm that:
- Your EC2 instance's security group allows outbound traffic on port 3306 to your RDS instance's security group.
- Your RDS security group allows inbound traffic on port 3306 from your EC2 instance's security group.
- The RDS instance is in a subnet that's accessible from your EC2 instance (same VPC, or proper peering if in different VPCs).
- If you're running MySQL locally on EC2, make sure the service is started:
sudo systemctl start mysqld sudo systemctl enable mysqld
4. Check the Full Error Stack Trace
The error message you shared is just the top-level exception. The nested exceptions below it will tell you the exact root cause (e.g., a missing database driver, class not found, or connection timeout). When you run java -jar <myfilename.jar>, scroll up/down in the console output to find the full stack trace—this will point you directly to what's breaking the startup.
5. Validate Jar File Integrity
It's possible the jar file got corrupted during upload to S3 or download to EC2. To confirm:
- On your local machine, calculate the MD5 hash of the jar:
md5sum <myfilename.jar> - In the AWS S3 console, navigate to your jar file, go to the Properties tab, and check the ETag value (if you uploaded the file in a single part, the ETag will match the MD5 hash).
- If the hashes don't match, re-upload the jar to S3 and download it again to EC2.
Try these steps one by one—usually, port conflicts or dependency issues are the quickest fixes. If you still run into problems, share the full stack trace and we can dig deeper!
内容的提问来源于stack exchange,提问作者Amit

