Skip to content
No description, website, or topics provided.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

This quickstart will get you going with Java and the Jetty embedded web server, deployed to Heroku.

{.note} Sample code for the Java demo application is available on GitHub. Edits and enhancements are welcome.


  • Basic Java knowledge, including an installed version of the JVM and Maven 3.
  • Your application must run on the OpenJDK version 6, or 7 (8 is also available in beta).
  • A Heroku user account. Signup is free and instant.

Local workstation setup

Install the Heroku Toolbelt on your local workstation. This ensures that you have access to the Heroku command-line client, Foreman, and the Git revision control system.

Once installed, you can use the heroku command from your command shell. Log in using the email address and password you used when creating your Heroku account:

$ heroku login
Enter your Heroku credentials.
Could not find an existing public key.
Would you like to generate one? [Yn] 
Generating new SSH public key.
Uploading ssh public key /Users/adam/.ssh/

Press enter at the prompt to upload your existing ssh key or create a new one, used for pushing code later on.

Write your app

You can run any Java application on Heroku that uses Maven as build tool. As an example, we will write a web app using Jetty. Here is a basic servlet class that also contains a main method to start up the application:


import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.*;

public class HelloWorld extends HttpServlet {

    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.getWriter().print("Hello from Java!\n");

    public static void main(String[] args) throws Exception{
        Server server = new Server(Integer.valueOf(System.getenv("PORT")));
        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.addServlet(new ServletHolder(new HelloWorld()),"/*");

Declare dependencies in pom.xml

Cedar recognizes Java apps by the existence of a pom.xml file. Here's an example pom.xml for the Java/Jetty app we created above.


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" 

Prevent build artifacts from going into revision control by creating this file:



Build and run your app locally

Build your app locally:

$ mvn package

As part of the build, Maven gathers dependencies and copies them into the directory target/dependency. Start you app locally by setting the PORT environment variable and running Java with all dependencies on the classpath:

On Mac & Linux:

$ export PORT=5000
$ java -cp target/classes:"target/dependency/*" HelloWorld

(double quotes needed to prevent expansion of *)

On Windows:

$ set PORT=5000
$ java -cp target\classes;"target\dependency\*" HelloWorld

You should now see something similar to:

2012-01-31 15:51:21.811:INFO:oejs.Server:jetty-7.6.0.v20120127
2012-01-31 15:51:21.931:INFO:oejsh.ContextHandler:started o.e.j.s.ServletContextHandler{/,null}
2012-01-31 15:51:21.971:INFO:oejs.AbstractConnector:Started SelectChannelConnector@

Open the app in your browser:

Declare process types with a Procfile

To run your web process on Heroku, you need to declare what command to use. We'll use Procfile to declare how our web process type is run.

Here's what the Procfile looks like:

web:    java -cp target/classes:target/dependency/* HelloWorld

(note: no double quotes needed in Procfile)

Optionally Choose a JDK

By default, OpenJDK 1.6 is installed with your app. However, you can choose to use a newer JDK by specifying java.runtime.version=1.7 in the file.

Here's what a file looks like:


You can specify 1.6, 1.7, or 1.8 (1.8 is in beta) for Java 6, 7, or 8 (with lambdas), respectively.

Store your app in Git

We now have the three major components of our app: build configuration and dependencies in pom.xml, process types in Procfile, and our application source in src/main/java/ Let's put it into Git:

$ git init
$ git add .
$ git commit -m "init"

Deploy to Heroku

Create the app:

$ heroku create
Creating stark-sword-398... done, stack is cedar |
Git remote heroku added

Deploy your code:

$ git push heroku master
Counting objects: 47, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (25/25), done.
Writing objects: 100% (47/47), 10.25 KiB, done.
Total 47 (delta 19), reused 42 (delta 17)

-----> Heroku receiving push
-----> Java app detected
-----> Installing OpenJDK 1.6... done
-----> Installing Maven 3.0.3... done
-----> Installing settings.xml... done
-----> executing /app/tmp/repo.git/.cache/.maven/bin/mvn -B -Duser.home=/tmp/build_3k0p14ghrmdzs -Dmaven.repo.local=/app/tmp/repo.git/.cache/.m2/repository -s /app/tmp/repo.git/.cache/.m2/settings.xml -DskipTests=true clean install
       [INFO] Scanning for projects...
       [INFO] ------------------------------------------------------------------------
       [INFO] Building helloworld 1.0-SNAPSHOT
       [INFO] ------------------------------------------------------------------------
       [INFO] ------------------------------------------------------------------------
       [INFO] ------------------------------------------------------------------------
       [INFO] Total time: 10.062s
       [INFO] Finished at: Tue Jan 31 23:27:20 UTC 2012
       [INFO] Final Memory: 12M/490M
       [INFO] ------------------------------------------------------------------------
-----> Discovering process types
       Procfile declares types -> web
-----> Compiled slug size is 948K
-----> Launching... done, v3 deployed to Heroku

Now, let's check the state of the app's processes:

$ heroku ps
Process  State       Command                               
-------  ----------  ------------------------------------  
web.1    up for 10s  java -cp target/classes:target/dep..  

The web process is up. Review the logs for more information:

$ heroku logs
2012-01-31T23:27:27+00:00 heroku[web.1]: Starting process with command `java -cp target/classes:target/dependency/* HelloWorld`
2012-01-31T23:27:28+00:00 app[web.1]: 2012-01-31 23:27:28.280:INFO:oejs.Server:jetty-7.6.0.v20120127
2012-01-31T23:27:28+00:00 app[web.1]: 2012-01-31 23:27:28.334:INFO:oejsh.ContextHandler:started o.e.j.s.ServletContextHandler{/,null}
2012-01-31T23:27:28+00:00 app[web.1]: 2012-01-31 23:27:28.373:INFO:oejs.AbstractConnector:Started SelectChannelConnector@
2012-01-31T23:27:29+00:00 heroku[web.1]: State changed from starting to up
2012-01-31T23:27:32+00:00 heroku[router]: GET dyno=web.1 queue=0 wait=0ms service=27ms status=200 bytes=17

Looks good. We can now visit the app with heroku open.

Next steps: database-driven apps

The Spring MVC Hibernate tutorial will guide you through setting up a database-driven application on Heroku.

You can’t perform that action at this time.