What is Heroku?

Heroku is a cloud application platform – a new way of building and deploying web apps. Our service lets app developers spend their time on their application code, not managing servers, deployment, ongoing operations, or scaling. — heroku.com

Heroku takes care of everything related to deployment, and gives you easy access to key commands via their tool Heroku Toolbelt. It’s very easy to get started with (as you’ll soon learn), and it provides a nice free-tier that you can use to deploy your webapps.

Initial Setup

Before we get started, there are a few things we need to do:

Configuring Maven

This is actually where most of the work is done. In order to easily deploy a Java application anywhere, you have to create a jar file containing your application and all of its dependencies. Open the pom.xml of your Javalin Maven project and add the following configuration (below your dependencies tag):

                    <!-- This tells Maven to include all dependencies -->

Configuring Heroku

Before we can configure anything, we actually have to create a Heroku application. This can be done by using the heroku create command.
Open a terminal and navigate to your project root, then enter:

heroku create javalin-heroku-example #choose your own application name 

Now that you have a Heroku application, we have to configure how to deploy it using Maven. This is pretty straightforward using the Heroku Maven plugin.

We specify the JDK version and the app-name, along with the launch config:

            <!-- Tell Heroku how to launch your application -->
            <web>java -jar ./target/javalin-heroku-example-1.0-jar-with-dependencies.jar</web>

When you’ve added the Heroku config to your pom, it should look like this

Making Javalin Listen on the Correct Port

The only thing left is making sure Javalin can handle your requests. Heroku assigns your application a new port every time you deploy it, so we have to get this port and tell Javalin to use it:

import io.javalin.Javalin;

public class Main {

    public static void main(String[] args) {
        Javalin app = Javalin.create()
            .get("/", ctx -> ctx.result("Hello Heroku"));

    private static int getHerokuAssignedPort() {
        ProcessBuilder processBuilder = new ProcessBuilder();
        if (processBuilder.environment().get("PORT") != null) {
            return Integer.parseInt(processBuilder.environment().get("PORT"));
        return 7000;


Now we can deploy our application using mvn heroku:deploy.

Again, make sure you’re in your project root, then enter:

mvn heroku:deploy

That’s it. Our application is now available at https://<appname>.herokuapp.com/