AWS EC2实例启动后Java程序无法运行问题求助
Let’s walk through fixing your Java applications step by step to get them up and running smoothly on your EC2 instance.
1. First, Confirm Java is Installed
You can’t run JAR files without a Java Runtime Environment (JRE) or JDK. Let’s check if it’s set up:
java -version
If you don’t see a version number pop up, install Java based on your EC2 OS:
- For Amazon Linux 2:
sudo yum install java-1.8.0-openjdk -y - For Ubuntu:
sudo apt update && sudo apt install openjdk-8-jre -y
2. Manually Launch the JARs to Catch Errors
Before setting up background execution, run each JAR directly in the terminal—this will show you exactly what’s going wrong. Start by navigating to your directory:
cd /home/ec2-user/559
Launch response_server.jar first (since it’s critical for ELB health checks):
java -jar response_server.jar
Do the same for server.jar:
java -jar server.jar
Watch for common errors here:
- "Port already in use": Another process is hogging port 5000 or 6000
- "Permission denied":
ec2-userdoesn’t have access to the JAR files - "No main manifest attribute": The JAR isn’t packaged correctly (unlikely if you expected it to work)
3. Fix Port Conflicts
If you get a port-in-use error, find the conflicting process with:
# Check port 5000 sudo netstat -tulpn | grep 5000 # Check port 6000 sudo netstat -tulpn | grep 6000
You’ll see a process ID (PID) in the output. Kill it with:
sudo kill -9 <PID>
Then try launching your JAR again.
4. Correct File Permissions
If you see permission errors, check who owns the JAR files:
ls -l /home/ec2-user/559
If they’re not owned by ec2-user, fix the permissions:
sudo chown ec2-user:ec2-user /home/ec2-user/559/*.jar
5. Run JARs in the Background (So They Stay Up)
Once you’ve confirmed the JARs run manually, you need them to keep running after you close your terminal. Use nohup to run them in the background and log output:
# Start response_server.jar and log to a file nohup java -jar response_server.jar > response_server.log 2>&1 & # Start server.jar and log to a file nohup java -jar server.jar > server.log 2>&1 &
Verify the processes are running:
ps aux | grep java
Check logs later if you need to debug runtime issues:
tail -f response_server.log tail -f server.log
6. Double-Check Network Access (For ELB Health Checks)
Once your apps are running, make sure your EC2 security group allows inbound traffic on port 6000 (for ELB health checks) and port 5000 (for client communication). Your subnet’s NACL should also allow these ports for both inbound and outbound traffic.
内容的提问来源于stack exchange,提问作者shrram

