You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

部署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:
    heroku ps --app <appname>
    
    You should see your web or worker dyno listed as up.
  • 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 --num flag:
    heroku logs --num 2000 --app <appname>
    

Quick Notes to Avoid Headaches

  • Ensure your JAR has a properly configured Main-Class in its manifest file—otherwise java -jar won't know where to start.
  • If you're running a web app, make sure your code listens on the port specified by Heroku's PORT environment variable (use System.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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:17:40