Getting Started with Gradle on Heroku
Introduction
Complete this tutorial to deploy a sample Java (Gradle) app to Cedar, the legacy generation of the Heroku platform. To deploy the app to the Fir generation, only available to Heroku Private Spaces, follow this guide instead.
The tutorial assumes that you have:
- A verified Heroku Account
- An Eco dynos plan subscription (recommended)
- OpenJDK 17 (or newer) installed locally
- Postgres installed locally
Using dynos and databases to complete this tutorial counts towards your usage. We recommend using our low-cost plans to complete this tutorial. Eligible students can apply for platform credits through our new Heroku for GitHub Students program.
Set Up
Install the Heroku Command Line Interface (CLI). Use the CLI to manage and scale your app, provision add-ons, view your logs, and run your app locally.
The Heroku CLI requires Git, the popular version control system. If you don’t already have Git installed, complete the following before proceeding:
Download and run the installer for your platform:
Download the appropriate installer for your Windows installation:
You can find more installation options for the Heroku CLI here.
After installation, you can use the heroku command from your command shell.
To log in to the Heroku CLI, use the heroku login command:
$ heroku login
heroku: Press any key to open up the browser to login or q to exit:
Opening browser to https://cli-auth.heroku.com/auth/cli/browser/***
heroku: Waiting for login...
Logging in... done
Logged in as me@example.com
This command opens your web browser to the Heroku login page. If your browser is already logged in to Heroku, click the Log In button on the page.
This authentication is required for the heroku and git commands to work correctly.
If you have any problems installing or using the Heroku CLI, see the main Heroku CLI article for advice and troubleshooting steps.
If you’re behind a firewall that uses a proxy to connect with external HTTP/HTTPS services, set the HTTP_PROXY or HTTPS_PROXY environment variables in your local development environment before running the heroku command.
Clone the Sample App
If you’re new to Heroku, it’s recommended that you complete this tutorial using the Heroku-provided sample application.
To deploy an existing application, follow this article instead.
Clone the sample application to get a local version of the code. Execute these commands in your local command shell or terminal:
$ git clone https://github.com/heroku/gradle-getting-started
$ cd gradle-getting-started
You now have a functioning Git repository that contains a simple application. It includes a build.gradle.kts file, which is used by Gradle, a Java build tool.
Create Your App
Using a dyno and a database to complete this tutorial counts towards your usage. Delete your app, and database as soon as you’re done to control costs.
Apps use Eco dynos if you’re subscribed to Eco by default. Otherwise, it defaults to Basic dynos. The Eco dynos plan is shared across all Eco dynos in your account. It’s recommended if you plan on deploying many small apps to Heroku. Learn more here. Eligible students can apply for platform credits through our Heroku for GitHub Students program.
Create an app on Heroku to prepare the platform to receive your source code:
$ heroku create
Creating app... done, blooming-tor-79820
https://blooming-tor-79820-6e25caf39760.herokuapp.com/ | https://git.heroku.com/blooming-tor-79820.git
When you create an app, a Git remote named heroku is also created and added to your local repository configuration. Git remotes are versions of your repository that live on other servers. You can deploy your app by pushing code to that special Heroku-hosted remote associated with your app.
Heroku generates a random name for your app, in this case, blooming-tor-79820. You can specify your own app name.
Provision a Database
The sample app requires a database. Provision a Heroku Postgres database, an add-on available through the Elements Marketplace. Add-ons are cloud services that provide out-of-the-box additional services for your application, such as logging, monitoring, databases, and more.
An essential-0 Postgres size costs $5 a month, prorated to the minute. At the end of this tutorial, we prompt you to delete your database to minimize costs.
$ heroku addons:create heroku-postgresql:essential-0
Creating heroku-postgresql:essential-0 on blooming-tor-79820... ~$0.007/hour (max $5/month)
Database should be available soon
postgresql-polished-82360 is being created in the background. The app will restart when complete...
Run heroku addons:info postgresql-polished-82360 to check creation progress.
Run heroku addons:docs heroku-postgresql to view documentation.
You can wait for the database to provision by running this command:
$ heroku pg:wait
After that command exits, your Heroku app can access the Postgres database. The DATABASE_URL environment variable stores your credentials. For applications that use the JVM an additional environment variable JDBC_DATABASE_URL is available. It contains a JDBC compatible connection string. You can see all the add-ons provisioned with the addons command:
$ heroku addons
Add-on Plan Price Max Price State
──────────────────────────────────────────────────────────────────────────────────────────────────
heroku-postgresql (postgresql-polished-82360) essential-0 ~$0.007/hour $5/month created
└─ as DATABASE
The table above shows add-ons and the attachments to the current app (blooming-tor-79820) or other apps.
Deploy the App
Using a dyno to complete this tutorial counts towards your usage. Delete your app and database as soon as you’re done to control costs.
Deploy your code. This command pushes the main branch of the sample repo to your heroku remote, which then deploys to Heroku:
$ git push heroku main
remote: Updated 24 paths from 71b2b0e
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Building on the Heroku-24 stack
remote: -----> Determining which buildpack to use for this app
remote: -----> Gradle app detected
remote: -----> Installing Azul Zulu OpenJDK 25.0.3
remote: -----> Starting Gradle daemon
remote: -----> Executing Gradle
remote: $ ./gradlew build -x check
remote: > Task :compileJava
remote: > Task :processResources
remote: > Task :classes
remote: > Task :resolveMainClassName
remote: > Task :bootJar
remote: > Task :jar
remote: > Task :assemble
remote: > Task :build
remote:
remote: BUILD SUCCESSFUL in 17s
remote: 5 actionable tasks: 5 executed
remote: Consider enabling configuration cache to speed up this build: https://docs.gradle.org/9.5.0/userguide/configuration_cache_enabling.html
remote: -----> Stopping Gradle daemon
remote: -----> Discovering process types
remote: Procfile declares types -> web
remote:
remote: -----> Compressing...
remote: Done: 107.5M
remote: -----> Launching...
remote: Released v4
remote: https://blooming-tor-79820-6e25caf39760.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/blooming-tor-79820.git
* [new branch] main -> main
Visit the app at the URL shown in the logs. As a shortcut, you can also open the website as follows:
$ heroku open
Understanding the Procfile
Use a Procfile, a text file in the root directory of your application, to explicitly declare what command to execute to start your app.
The Procfile in the example app looks like this:
web: java -jar build/libs/java-getting-started-gradle-1.0.0-SNAPSHOT.jar
This Procfile declares a single process type, web, and the command needed to run it. The name web is important here. It declares that this process type is attached to Heroku’s HTTP routing stack and receives web traffic when deployed.
A Procfile can contain additional process types. For example, you can declare a background worker process that processes items off a queue.
View Logs
Heroku treats logs as streams of time-ordered events, aggregated from the output streams of all your app and Heroku components. Heroku provides a single stream for all events.
View information about your running app by using one of the logging commands, heroku logs --tail:
$ heroku logs --tail
2026-05-05T22:03:06.613966+00:00 app[api]: Initial release by user developer@example.com
2026-05-05T22:03:06.613966+00:00 app[api]: Release v1 created by user developer@example.com
2026-05-05T22:03:06.960525+00:00 app[api]: Release v2 created by user developer@example.com
2026-05-05T22:03:06.960525+00:00 app[api]: Enable Logplex by user developer@example.com
2026-05-05T22:04:23.524773+00:00 app[api]: @ref:postgresql-polished-82360 completed provisioning, setting DATABASE_URL. by user heroku-postgresql@addons.heroku.com
2026-05-05T22:04:23.524773+00:00 app[api]: Release v3 created by user heroku-postgresql@addons.heroku.com
2026-05-05T22:04:28.000000+00:00 app[api]: Build started by user developer@example.com
2026-05-05T22:05:43.651440+00:00 app[api]: Deploy 85d0a065 by user developer@example.com
2026-05-05T22:05:47.640234+00:00 heroku[web.1]: Starting process with command `java -jar build/libs/java-getting-started-gradle-1.0.0-SNAPSHOT.jar`
2026-05-05T22:05:48.463868+00:00 app[web.1]: Setting JAVA_TOOL_OPTIONS defaults based on dyno size. Custom settings will override them.
2026-05-05T22:05:48.466808+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8 -XX:MaxRAM=536870912 -Xmx300m -Xss512k -XX:CICompilerCount=2
2026-05-05T22:05:49.203174+00:00 app[web.1]: _ _ _
2026-05-05T22:05:49.203193+00:00 app[web.1]: | | | | | |
2026-05-05T22:05:49.203194+00:00 app[web.1]: | |__| | ___ _ __ ___ | | ___ _
2026-05-05T22:05:49.203194+00:00 app[web.1]: | __ |/ _ \ '__/ _ \| |/ / | | |
2026-05-05T22:05:49.203195+00:00 app[web.1]: | | | | __/ | | (_) | <| |_| |
2026-05-05T22:05:49.203195+00:00 app[web.1]: |_| |_|\___|_| \___/|_|\_\\__,_|
2026-05-05T22:05:49.203195+00:00 app[web.1]:
2026-05-05T22:05:49.203196+00:00 app[web.1]: :: Built with Spring Boot :: 4.0.6
2026-05-05T22:05:49.203196+00:00 app[web.1]:
2026-05-05T22:05:49.267785+00:00 app[web.1]: 2026-05-05T22:05:49.265Z INFO 2 --- [ main] c.heroku.java.GettingStartedApplication : Starting GettingStartedApplication v1.0.0-SNAPSHOT using Java 25.0.3 with PID 2 (/app/build/libs/java-getting-started-gradle-1.0.0-SNAPSHOT.jar started by u47902 in /app)
2026-05-05T22:05:49.268677+00:00 app[web.1]: 2026-05-05T22:05:49.268Z INFO 2 --- [ main] c.heroku.java.GettingStartedApplication : No active profile set, falling back to 1 default profile: "default"
2026-05-05T22:05:50.050863+00:00 app[web.1]: 2026-05-05T22:05:50.050Z INFO 2 --- [ main] o.s.boot.tomcat.TomcatWebServer : Tomcat initialized with port 41866 (http)
2026-05-05T22:05:50.062753+00:00 app[web.1]: 2026-05-05T22:05:50.062Z INFO 2 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2026-05-05T22:05:50.062929+00:00 app[web.1]: 2026-05-05T22:05:50.062Z INFO 2 --- [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/11.0.21]
2026-05-05T22:05:50.085116+00:00 app[web.1]: 2026-05-05T22:05:50.084Z INFO 2 --- [ main] b.w.c.s.WebApplicationContextInitializer : Root WebApplicationContext: initialization completed in 766 ms
2026-05-05T22:05:50.201412+00:00 app[web.1]: 2026-05-05T22:05:50.201Z INFO 2 --- [ main] o.s.b.w.a.WelcomePageHandlerMapping : Adding welcome page template: index
2026-05-05T22:05:50.457036+00:00 app[web.1]: 2026-05-05T22:05:50.456Z INFO 2 --- [ main] o.s.boot.tomcat.TomcatWebServer : Tomcat started on port 41866 (http) with context path '/'
2026-05-05T22:05:50.467462+00:00 app[web.1]: 2026-05-05T22:05:50.467Z INFO 2 --- [ main] c.heroku.java.GettingStartedApplication : Started GettingStartedApplication in 1.571 seconds (process running for 2.0)
2026-05-05T22:05:50.483879+00:00 heroku[web.1]: State changed from starting to up
2026-05-05T22:06:07.716513+00:00 app[web.1]: 2026-05-05T22:06:07.716Z INFO 2 --- [io-41866-exec-3] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2026-05-05T22:06:07.716666+00:00 app[web.1]: 2026-05-05T22:06:07.716Z INFO 2 --- [io-41866-exec-3] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2026-05-05T22:06:07.718130+00:00 app[web.1]: 2026-05-05T22:06:07.717Z INFO 2 --- [io-41866-exec-3] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
2026-05-05T22:06:07.932151+00:00 heroku[router]: at=info method=GET path="/" host=blooming-tor-79820-6e25caf39760.herokuapp.com request_id=1e0b5215-eeb7-260e-56b3-55d227b1d995 fwd="123.456.789.0" dyno=web.1 connect=0ms service=235ms status=200 bytes=10069 protocol=http1.1 tls=false
2026-05-05T22:05:43.664986+00:00 app[api]: Scaled to web@1:Basic by user developer@example.com
2026-05-05T22:05:43.651440+00:00 app[api]: Release v4 created by user developer@example.com
2026-05-05T22:05:57.000000+00:00 app[api]: Build succeeded
To generate more log messages, refresh the app in your browser.
To stop streaming the logs, press Control+C.
Scale the App
After deploying the sample app, it automatically runs on a single web dyno. Think of a dyno as a lightweight container that runs the command specified in the Procfile.
You can check how many dynos are running by using the ps command:
$ heroku ps
=== web (Basic): java -jar build/libs/java-getting-started-gradle-1.0.0-SNAPSHOT.jar (1)
web.1: up 2026/05/05 17:05:50 -0500 (~ 29s ago)
Scaling an app on Heroku is equivalent to changing the number of running dynos. Scale the number of web dynos to zero:
$ heroku ps:scale web=0
$ heroku ps:wait
Access the app again by hitting refresh in your browser, or heroku open to open it in a web tab. You get an error message because you no longer have web dynos available to serve requests.
Scale it up again:
$ heroku ps:scale web=1
$ heroku ps:wait
By default, apps use Eco dynos if you’re subscribed to Eco. Otherwise, it defaults to Basic dynos. The Eco dynos plan is shared across all Eco dynos in your account and is recommended if you plan on deploying many small apps to Heroku. Eco dynos sleep if they don’t receive any traffic for half an hour. This sleep behavior causes a few seconds delay for the first request upon waking. Eco dynos consume from a monthly, account-level quota of eco dyno hours. As long as you haven’t exhausted the quota, your apps can continue to run.
To avoid dyno sleeping, upgrade to a Basic or higher dyno type as described in the Dyno Types article. Upgrading to at least Standard dynos allows you to scale up to multiple dynos per process type.
Declare App Dependencies
Heroku recognizes an app as a Java (Gradle) app by the existence of a build.gradle.kts or build.gradle file in the root directory.
The demo app you deployed already has a build.gradle.kts:
plugins {
java
id("org.springframework.boot") version "4.0.6"
id("io.spring.dependency-management") version "1.1.7"
}
group = "com.heroku"
version = "1.0.0-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_17
repositories {
...
The build.gradle.kts file specifies the dependencies to install with your application.
Run ./gradlew build in your local directory to install the dependencies, preparing your system for running the app locally:
$ ./gradlew build
Starting a Gradle Daemon, 1 incompatible and 1 stopped Daemons could not be reused, use --status for details
> Task :compileJava
> Task :processResources
> Task :classes
> Task :resolveMainClassName
Push Local Changes
In this step, you propagate a local change to the application to Heroku.
Modify build.gradle.kts to include an additional dependency for the jscience.
In file build.gradle.kts, on line 16 add:
implementation("org.jscience:jscience:4.3.1")
Add the import statements for the library.
In file src/main/java/com/heroku/java/GettingStartedApplication.java, on line 2 add:
import org.jscience.physics.amount.Amount;
import org.jscience.physics.model.RelativisticModel;
import javax.measure.unit.SI;
Add a new convert method.
In file src/main/java/com/heroku/java/GettingStartedApplication.java, on line 27 add:
@GetMapping("/convert")
String convert(Map<String, Object> model) {
RelativisticModel.select();
var energy = Amount.valueOf("12 GeV");
model.put("result", "E=mc^2: " + energy + " = " + energy.to(SI.KILOGRAM));
return "convert";
}
Add a new template.
In file src/main/resources/templates/convert.html write:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" th:replace="~{fragments/layout :: layout (~{::body},'hello')}">
<body>
<div class="container">
<p th:text="${result}"/>
</div>
</body>
</html>
Now test locally:
$ ./gradlew build
$ heroku local --port=5006
Visit your application’s /convert path at http://localhost:5006/convert. If your changes worked, you see scientific conversions:
E=mc^2: 12 GeV = (2.139194076302506E-26 ± 1.4E-42) kg
Now deploy this local change to Heroku.
Almost every deploy to Heroku follows this same pattern. First, add the modified files to the local Git repository:
$ git add .
Now commit the changes to the repository:
$ git commit -m "Add convert endpoint"
[main 1c4468d] Add convert endpoint
3 files changed, 22 insertions(+)
create mode 100644 src/main/resources/templates/convert.html
Now deploy as before:
$ git push heroku main
Finally, check that everything is working:
$ heroku open /convert
Provision a Logging Add-on
Beyond databases, add-ons provide many additional services for your application. In this step, you provision a free add-on to store your app’s logs.
By default, Heroku stores 1500 lines of logs from your application, but the full log stream is available as a service. Several add-on providers have logging services that provide things such as log persistence, search, and email and SMS alerts.
In this step, you provision one of these logging add-ons, Papertrail.
Provision the Papertrail logging add-on:
$ heroku addons:create papertrail
Creating papertrail on blooming-tor-79820... free
Provisioning has been successful
Created papertrail-vertical-75389
Run heroku addons:docs papertrail to view documentation.
The add-on is now deployed and configured for your app. You can list add-ons for your app with this command:
$ heroku addons
Add-on Plan Price Max Price State
──────────────────────────────────────────────────────────────────────────────────────────────────
heroku-postgresql (postgresql-polished-82360) essential-0 ~$0.007/hour $5/month created
└─ as DATABASE
papertrail (papertrail-vertical-75389) choklad free free created
└─ as PAPERTRAIL
The table above shows add-ons and the attachments to the current app (blooming-tor-79820) or other apps.
To see this particular add-on in action, visit your application’s Heroku URL a few times. Each visit generates more log messages, which get routed to the Papertrail add-on. Visit the Papertrail console to see the log messages:
$ heroku addons:open papertrail
Your browser opens up a Papertrail web console, showing the latest log events. The interface lets you search and set up alerts.
Start a One-off Dyno
You can run a command, typically scripts and applications that are part of your app, in a one-off dyno using the heroku run command. You can also run a one-off command like java -version:
$ heroku run "java -version"
Running java -version on blooming-tor-79820... starting, run.8302
Running java -version on blooming-tor-79820... connecting, run.8302
Running java -version on blooming-tor-79820... up, run.8302
openjdk version "25.0.3" 2026-04-21 LTS
OpenJDK Runtime Environment Zulu25.34+17-CA (build 25.0.3+9-LTS)
OpenJDK 64-Bit Server VM Zulu25.34+17-CA (build 25.0.3+9-LTS, mixed mode, sharing)
If you receive an error, Error connecting to process, configure your firewall.
Let’s try another example. Run the bash command to open up a shell on a one-off dyno. You can then execute commands there.
$ heroku run bash
Running bash on blooming-tor-79820... starting, run.3188
Running bash on blooming-tor-79820... connecting, run.3188
Running bash on blooming-tor-79820... up, run.3188
~ $ ls -lah
total 88K
drwx------ 9 u25513 dyno 4.0K May 5 22:07 .
drwxr-xr-x 11 root root 4.0K Apr 22 14:59 ..
-rw------- 1 u25513 dyno 14 May 5 22:04 .env
drwx------ 2 u25513 dyno 4.0K May 5 22:04 .github
-rw------- 1 u25513 dyno 333 May 5 22:04 .gitignore
drwx------ 6 u25513 dyno 4.0K May 5 22:05 .heroku
drwx------ 8 u25513 dyno 4.0K May 5 22:04 .jdk
drwx------ 2 u25513 dyno 4.0K May 5 22:04 .profile.d
-rw------- 1 u25513 dyno 1.1K May 5 22:04 LICENSE
-rw------- 1 u25513 dyno 73 May 5 22:04 Procfile
-rw------- 1 u25513 dyno 2.5K May 5 22:04 README.md
-rw------- 1 u25513 dyno 155 May 5 22:04 app.json
drwx------ 7 u25513 dyno 4.0K May 5 22:05 build
-rw------- 1 u25513 dyno 619 May 5 22:04 build.gradle.kts
drwx------ 3 u25513 dyno 4.0K May 5 22:04 gradle
-rwx------ 1 u25513 dyno 8.5K May 5 22:04 gradlew
-rwx------ 1 u25513 dyno 2.8K May 5 22:04 gradlew.bat
-rw------- 1 u25513 dyno 49 May 5 22:04 settings.gradle.kts
drwx------ 3 u25513 dyno 4.0K May 5 22:04 src
-rw------- 1 u25513 dyno 103 May 5 22:04 system.properties
~ $
~ $ exit
exit
Type exit to exit the shell.
Define Config Vars
Heroku lets you externalize configuration by storing data such as encryption keys or external resource addresses in config vars.
At runtime, we expose config vars as environment variables to the application.
For example, modify GettingStartedApplication.java so that the method obtains an energy value from the ENERGY environment variable:
@GetMapping("/convert")
String convert(Map<String, Object> model) {
RelativisticModel.select();
final var result = java.util.Optional
.ofNullable(System.getenv().get("ENERGY"))
.map(Amount::valueOf)
.map(energy -> "E=mc^2: " + energy + " = " + energy.to(SI.KILOGRAM))
.orElse("ENERGY environment variable is not set!");
model.put("result", result);
return "convert";
}
The heroku local command automatically sets up the environment based on the contents of the .env file in your local directory. In the top level directory of your sample project, there’s already a .env file that contains:
ENERGY=20 GeV
Rebuild the app with ./gradlew build. Then run the app with heroku local --port=5006 and visit http://localhost:5006/convert to see the conversion value for 20 GeV.
To set the config var on Heroku, execute the following:
$ heroku config:set ENERGY="20 GeV"
Setting ENERGY and restarting blooming-tor-79820... done, v5
ENERGY: 20 GeV
View the app’s config vars using heroku config:
$ heroku config
ENERGY: 20 GeV
...
To see this change in action, deploy your changed application to Heroku.
Use a Database
Listing the config vars for your app displays the URL that your app uses to connect to the database, DATABASE_URL:
$ heroku config
DATABASE_URL: postgres://xx:yyy@host:5432/d8slm9t7b5mjnd
HEROKU_POSTGRESQL_BROWN_URL: postgres://xx:yyy@host:5432/d8slm9t7b5mjnd
...
Heroku also provides a pg command that shows a lot more information:
$ heroku pg
=== DATABASE_URL
Plan: essential-0
Status: Available
Connections: unknown/20
PG Version: 17.9
Created: 2026-05-05 22:03
Data Size: unknown usage / 1 GB (In compliance)
Tables: 0/4000 (In compliance)
Fork/Follow: Unsupported
Rollback: Unsupported
Continuous Protection: On
Add-on: postgresql-polished-82360
The example app you deployed already has database functionality. You can visit the page by appending /database to your app’s URL.
Database Output
* Read from DB: 2024-11-27 13:07:53.002632
* Read from DB: 2024-11-27 13:07:54.965283
* Read from DB: 2024-11-27 13:07:55.620596
If you have Postgres installed locally, you can also interact directly with the database. For example, here’s how to connect to the database using psql and execute a query:
$ heroku pg:psql -c "SELECT * FROM ticks"
--> Connecting to postgresql-polished-82360
tick
----------------------------
2026-05-05 22:07:43.229628
(1 row)
Read more about Heroku PostgreSQL.
Delete Your App
Remove the app from your account. We only charge you for the resources you used.
This action permanently deletes your application and any add-ons attached to it.
$ heroku apps:destroy
You can confirm that your app is gone with this command:
$ heroku apps --all
Next Steps
You now know how to configure and deploy a Java app, view logs, and start a console.
To learn more, see: