Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
262 lines (205 sloc) 10.1 KB

This quickstart will get you going with Java and the Jetty embedded web server on the Cedar stack.

Sample code is available on github along with this document. Edits and enhancements are welcome. Just fork the repository, make your changes and send us a pull request.


Local Workstation Setup

We'll start by setting up your local workstation with the Heroku command-line client and the Git revision control system; and then logging into Heroku to upload your ssh public key. If you've used Heroku before and already have a working local setup, skip to the next section.

If you have... Install with...
Mac OS X Download OS X package
Windows Download Windows .exe installer
Ubuntu Linux apt-get repository
Other Tarball (add contents to your $PATH)

Once installed, you'll have access to 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. The maven-appassembler-plugin generates an execution wrapper with the correct CLASSPATH.


<?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

Start you app locally by setting the PORT environment variable and running the generated webapp script:

On Mac & Linux:

$ export PORT=5000
$ sh target/bin/webapp

On Windows:

$ set PORT=5000
$ target\bin\webapp.bat

You should now see something similar to:

2011-08-18 15:52:24.066:INFO::jetty-7.4.5.v20110725
2011-08-18 15:52:24.142:INFO::started o.e.j.s.ServletContextHandler{/,null}
2011-08-18 15:52:24.168:INFO::Started SelectChannelConnector@ START

Open the app in your browser:

Declare Process Types With 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. The appassembler plugin takes care of generating a run script, target/bin/webapp, which we'll use to start the web app.

Here's what the Procfile looks like:

web: sh target/bin/webapp

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/Cedar

Create the app on the Cedar stack:

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

Deploy your code:

$ git push heroku master
Counting objects: 9, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (9/9), 1.37 KiB, done.
Total 9 (delta 0), reused 0 (delta 0)

-----> Heroku receiving push
-----> Java app detected
-----> Installing Maven 3.0.3..... done
-----> executing .maven/bin/mvn -B -Duser.home=/tmp/build_1cq2vqzdjg7yh -DskipTests=true clean install
       [INFO] Scanning for projects...
       [INFO] ------------------------------------------------------------------------
       [INFO] Building helloworld 1.0-SNAPSHOT
       [INFO] ------------------------------------------------------------------------
       [INFO] ------------------------------------------------------------------------
       [INFO] ------------------------------------------------------------------------
       [INFO] Total time: 25.671s
       [INFO] Finished at: Thu Aug 18 05:22:18 UTC 2011
       [INFO] Final Memory: 10M/225M
       [INFO] ------------------------------------------------------------------------
-----> Discovering process types
       Procfile declares types -> web
-----> Compiled slug size is 12.4MB
-----> Launching... done, v5 deployed to Heroku

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

$ heroku ps
Process       State               Command
------------  ------------------  ------------------------------
web.1         up for 10s          sh target/bin/webapp

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

$ heroku logs
2011-08-18T05:30:55+00:00 heroku[web.1]: Starting process with command `java -Xmx384m -Xss256k -XX:+UseCompressedOops -classpath target/classes:"target/dependency/*" HelloWorld`
2011-08-18T05:30:56+00:00 app[web.1]: 2011-08-18 05:30:56.310:INFO::jetty-7.4.5.v20110725
2011-08-18T05:30:56+00:00 app[web.1]: 2011-08-18 05:30:56.353:INFO::started o.e.j.s.ServletContextHandler{/,null}
2011-08-18T05:30:56+00:00 app[web.1]: 2011-08-18 05:30:56.389:INFO::Started SelectChannelConnector@ STARTING
2011-08-18T05:30:56+00:00 heroku[web.1]: State changed from starting to up

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

Next Step: Database-driven Apps

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