Today Heroku announced that Java is now supported on the Heroku Cloud Application Platform! This is incredibly exciting news and I’m very lucky to be a Heroku for Java Developer Evangelist!
Joining salesforce.com and jumping into the the Java Cloud space holds some nostalgia for me. When I began using Java in 1997 I was working at an ISP in Denver. We did the regular web hosting thing, but when the first Java Servlet engines (like Java Web Server 1.0) came out, I created the “wantjava.com” hosting service. Things were really nasty at first. We could only run one instance of the JWS on a server so I came up with a really bad way to do “multi-tenancy”. I setup a cron job to rsync the customers’ .class files into the server’s webapp and then restart the server. Customers had to email me to get a servlet added to the web.xml file. Uggg… I feel like I need to go to confession for this. But it worked and as the Servlet containers improved we quickly migrated to a more sustainable model.
Now thirteen years later I am privileged to once again be part of Java on the Cloud. But this time around things are so much easier, better, and sexier! Heroku is a leading the way in a new generation of application deployment that is making things much better for us Java developers.
What is Heroku?
Shortly I will dive into how you can run Java on Heroku, but first, what is Heroku? From my perspective, Heroku is a Polyglot Cloud Application Platform. Heroku provides us a way to run Ruby, Node.js, Clojure, and Java applications on a managed, scalable, and multi-tenant system. Heroku also provides numerous add-ons that help us make the shift from monolithic middleware to Cloud Components. Another way to say it is:
Heroku = Polyglot + Platform as a Service (PaaS) + Cloud Components
It is very exciting to see these three things coming together! With Polyglot I can choose the right tool for the job. With PaaS I don’t have to think about managing operating systems, scalability, failover, etc. And with the Cloud Component Architecture I can keep my app thin and focused on what is unique to the problem it needs to solve. Heroku brings these models together as a cloud application platform.
Running Java Apps on Heroku
Heroku can run any Java app that runs in OpenJDK 6. Today Heroku uses Maven to create a “slug” for Java apps. That slug can then be loaded onto one or more “dynos“. You can tell a dyno to execute / start a Java app from the command line and you can also use a “Procfile” to provide a command that will auto-start for each instance of a specific dyno type. Web dynos are able to listen on a port and will receive HTTP traffic through a load balancer that is automatically setup for each app. With that background knowledge, lets dive into code!
For Dreamforce 2011, I (with the help of a few co-workers) put together a Heroku for Java Workbook. The Workbook provides detailed instructions on how to create web apps, connect to a database, setup worker processes, use the Redis to Go Heroku add-on, and use Spring Roo on Heroku. But if you are anxious to get started and don’t need as much hand-holding, here is a quick and very simple walk through of how to run Java on Heroku:
- Install the heroku command line client on Linux, Mac, or Windows.
- Install git and setup your ssh key
- Install Maven
- Login to Heroku from the command line:
- Create a new project directory and move into it:
- Create a Maven build file named pom.xml containing:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
- Create a Java source directory:
mkdir -p src/main/java
- Create a new Java class in the src/main/java directory named Hello.java containing:
public class Hello
public static void main(String args)
- Compile the class:
- Run the class locally:
java -cp target/classes Hello
- Create a local git repo, add the pom.xml file & src dir, and commit the files:
git add pom.xml src
git commit -m init
- Create a new app on Heroku using the Cedar stack:
heroku create -s cedar
- Upload your app to Heroku:
git push heroku master
Heroku will create a slug for your app.
- Run the app on Heroku:
heroku run "java -cp target/classes Hello"
Heroku will start a new dyno with your slug and then run the specified command.
You just ran Java on the cloud! Obviously this is a very simple example. But I like to start new things with the simplest thing that could possibly work. Now that you have that working there is more to learn and much more power to harness!
- Go through the Heroku for Java Workbook
- Go through the articles on the Heroku Dev Center
- Ask questions about Heroku on StackOverflow
- Keep watching here for many more blogs about Java on Heroku
Have fun and please let me know if you have any questions about Heroku.