部署Jar包至Heroku后如何带参运行及验证使用正确性?
Hey there! Let's walk through exactly how to run your JAR with custom parameters on Heroku, verify your setup, and grab those logs you need—here's the breakdown:
1. Set Up Your Startup Command (One-Time or Persistent)
First, you have two options depending on whether you need to run the JAR once or keep it running long-term:
For One-Time Execution (e.g., a batch job that finishes)
Skip the Procfile and run the command directly on a temporary Heroku dyno—this works just like your local java -jar command:
heroku run java -jar <filename>.jar <param1> <param2> --app <appname>
The output will print directly to your terminal as it runs.
For Persistent Execution (e.g., a web app or background worker)
You'll need a Procfile to define the permanent startup command:
- Create a file named Procfile (no file extension, capital P) in your project root.
- Add the line matching your use case:
# For web apps that need to listen for HTTP traffic web: java -jar <filename>.jar <param1> <param2> # For background tasks (no web port required) worker: java -jar <filename>.jar <param1> <param2> - Commit and push this change to Heroku:
git add Procfile git commit -m "Add Procfile with custom startup parameters" git push heroku main - Start the dyno to run your command:
# For web dynos heroku ps:scale web=1 --app <appname> # For worker dynos heroku ps:scale worker=1 --app <appname>
2. Verify Your Heroku App Setup
To make sure everything's configured correctly:
- Check your running dynos:
You should see yourheroku ps --app <appname>weborworkerdyno listed asup. - Confirm basic app details (like the correct app name and URL):
heroku info --app <appname>
3. Grab Execution Logs
Whether you ran a one-time job or a persistent dyno, here's how to get the logs:
- Live streaming logs (great for watching the process run in real time):
heroku logs --tail --app <appname> - Historical logs (view past output, default shows recent 100 lines):
heroku logs --app <appname> - To see more historical logs, add the
--numflag:heroku logs --num 2000 --app <appname>
Quick Notes to Avoid Headaches
- Ensure your JAR has a properly configured
Main-Classin its manifest file—otherwisejava -jarwon't know where to start. - If you're running a web app, make sure your code listens on the port specified by Heroku's
PORTenvironment variable (useSystem.getenv("PORT")in your code to grab it). Heroku expects web dynos to bind to this port, or it'll mark the dyno as crashed.
Let me know if you run into any issues—happy to help tweak things further!
内容的提问来源于stack exchange,提问作者Joss MT

