Using J2V8 with Heroku

Using J2V8 with Heroku

J2V8 is a set of Java bindings for the popular V8 JavaScript engine. In this tutorial we will explore how to use J2V8 as a JavaScript engine on Heroku.


Heroku is a cloud based Platform as a Service (PaaS) supporting several programming languages. Heroku originally started as a platform for hosting Ruby, but today it supports many languages including Java. With Java, you simply push your sources to Heroku, along with a maven pom file, and Heroku will build, deploy and manage your application. There are add-ons for integrating databases, push notifications, log aggregation and more.


V8 is a JavaScript runtime developed by Google and written in C++.  V8 is highly performant and is the workhorse behind the Chrome web browser. We developed J2V8 to bring the power and performance of V8 to the Java platform. J2V8 wraps V8 as a native library and exposes many of the V8 APIs to Java via the Java Native Interface (JNI).

J2V8 is available in Maven Central, and the pre-packaged jar file contains the native libraries. However, because the native libraries are platform dependent, several flavours of J2V8 are available. Each flavour is appropriate for a particular platform such as Android Arm, Android x86, Linux 64 bit, etc…

Developing Against J2V8

J2V8 is available in Maven Central and the Maven Coordinates for J2V8 specify the platform. For example, if you are developing on Windows, your pom.xml might look like this:


You can now begin using J2V8 in your Java applications. In this example we will execute a script and return the result during a doGet request.
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  V8 runtime = V8.createV8Runtime();
  String stringScript = runtime.executeStringScript("'Hello from JS!'");
  resp.getWriter().print("Hello from Java! " + stringScript);

However, if you colleagues are developing on other platforms, then this particular dependency won’t help them.

To help with this, we have created an all-in-one download that contains the native code for each platform. Un-tar the file, and point the java.library.path at that directory. J2V8 will load natives from the library path before the ones contained within the jar. This can be done with Heroku in the Procfile:

web:    java -Djava.library.path=/path/to/natives -cp target/classes:target/dependency/* Main

Deploying on Heroku

To deploy a Java application that uses J2V8, update your pom.xml to use 64 bit linux . Since J2V8 can self extract the native libraries and because 64 bit linux is architecture used on the Heroku servers, by updating your pom.xml you can simply push your sources to the Heroku and it will handle the rest. If you set your java.library.path in the Procfile, then you can run local instances on any architecture too.


A local deployment can be triggered by first building the application and then starting foreman.

[%] mvn clean install
[%] foreman start web

For deployments on Heroku hardware, push your sources to the Heroku remote and open the application.

[%] git push heroku master
[%] heroku open

Screen Shot 2015-06-04 at 2.26.53 PM

For more information on J2V8, follow me on Twitter.