Skip to content
Test app to see how Heroku reacts with a Gradle project (as opposed to Maven)
Java Groovy
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 apps on the Heroku Cedar stack using the Gradle build system and Jetty embedded web server.

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


  • Basic Java knowledge, including an installed version of the JVM.
  • Basic Gradle knowledge, including an installed version of Gradle (1.0-milestone-5 or later).
  • Your application must run on the OpenJDK version 6.
  • A Heroku user account. Signup is free and instant.

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

We will be creating a completely standard Java application that serves web requests using the Servlet API and the embedded Jetty web server.

First create a class that implements a simple Servlet. For the purpose of this example, we'll also create the main method in this class.


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()),"/*");

Set up the build

Create a Gradle build file in your project root. The stage task will be run by Heroku to build your app:


apply plugin:'java'
apply plugin:'application'

mainClassName = "HelloWorld"
applicationName = "app"

repositories {

dependencies {
    compile 'org.eclipse.jetty:jetty-servlet:7.4.5.v20110725'
    compile 'javax.servlet:servlet-api:2.5'

task stage(dependsOn: ['clean', 'installApp'])

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



Build Your App

Build your app locally:

$ gradle stage

Declare Process Types With Foreman/Procfile

To run your web process, you need to declare what command to use. We'll use Procfile to declare how our web process type is run. The application plugin takes care of generating a run script, build/install/app/bin/app, which we'll use to start the web app.

Here's what the Procfile looks like:

web: ./build/install/app/bin/app

Now that you have a Procfile, you can start your application with Foreman:

$ foreman start
11:01:26 web.1     | started with pid 68657
11:01:27 web.1     | 2011-10-13 11:01:27.033:INFO:oejs.Server:jetty-7.5.3.v20111011
11:01:27 web.1     | 2011-10-13 11:01:27.229:INFO:oejsh.ContextHandler:started o.e.j.s.ServletContextHandler{/,null}
11:01:27 web.1     | 2011-10-13 11:01:27.269:INFO:oejs.AbstractConnector:Started SelectChannelConnector@ STARTING

Your app will come up on port 5000, the default port set by Foreman. Test that it's working with curl or a web browser, then Ctrl-C to exit.

Store Your App in Git

We now have the three major components of our app: build configuration and dependencies in build.gradle, 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 -s cedar
Creating evening-sky-2099... done, stack is cedar |

Deploy your code:

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

-----> Heroku receiving push
-----> Java/Gradle app detected
-----> Installing gradle-1.0-milestone-5..... done
       (Use the Gradle Wrapper if you want to use a different gradle version)
-----> executing gradle -I /tmp/asdre342qfad/opt/init.gradle stage
       :processResources UP-TO-DATE
       Total time: 20.534 secs
-----> Discovering process types
       Procfile declares types -> web
-----> Compiled slug size is 936K
-----> 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 1m           ./build/install/app/bin/app

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

$ heroku logs
2011-10-13T18:06:23+00:00 heroku[api]: Deploy 7c70d13 by
2011-10-13T18:06:23+00:00 heroku[api]: Release v5 created by
2011-10-13T18:06:24+00:00 heroku[slugc]: Slug compilation finished
2011-10-13T18:06:43+00:00 heroku[web.1]: Unidling
2011-10-13T18:06:43+00:00 heroku[web.1]: State changed from down to created
2011-10-13T18:06:43+00:00 heroku[web.1]: State changed from created to starting
2011-10-13T18:06:44+00:00 heroku[web.1]: Starting process with command `./build/install/app/bin/app`
2011-10-13T18:06:45+00:00 app[web.1]: 2011-10-13 18:06:45.198:INFO:oejs.Server:jetty-7.5.3.v20111011
2011-10-13T18:06:45+00:00 app[web.1]: 2011-10-13 18:06:45.258:INFO:oejsh.ContextHandler:started o.e.j.s.ServletContextHandler{/,null}
2011-10-13T18:06:45+00:00 app[web.1]: 2011-10-13 18:06:45.293:INFO:oejs.AbstractConnector:Started SelectChannelConnector@ STARTING
2011-10-13T18:06:46+00:00 heroku[web.1]: State changed from starting to up

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

You can’t perform that action at this time.