Announcing J2V8 2.0

Last year I announced J2V8, a new JavaScript engine for Java that wraps V8 with a set of Java Bindings. We have been using this technology to power Tabris.js on Android — giving us much better performance than Rhino could. While J2V8 was very stable, it wasn’t very easy to consume. Today I’m happy to announce that J2V8 2.0 has shipped, and it’s much easier to embed in your Java Applications.

There are several notable improvements in J2V8 2.0:

Self Loading Native Libraries

Following the lead of SWT from years ago, we have included a Library Loader that will load the native library automatically for you. You no longer need to twiddle with the java.library.path to get J2V8 to work. This also works on Android.

Bundled Native Libraries

The native libraries have also been bundled with the Jar file. Currently we are shipping 5 different Jar files for J2V8:

The final one (j2v8_android.jar) contains the native library for both armv7l and x86, so you can use it on several different Android devices. By adding the jar to your classpath and calling V8 runtime = V8.createV8Runtime(); (V8.createV8Runtime(null, activity.getApplicationInfo().dataDir); on Android: because you need to specify the location to unpack the native library) the native library will be automatically loaded for you. We can add more platforms, but each one takes time as I need to build V8 on the platform first. Any particular platform I should target next?

Available in Maven Central

J2V8 is now available in Maven Central. You no longer need to build the library yourself. For example, add the following snippet to your pom.xml to use J2V8 on MacOS:

<pre lang="xml"><dependency>
  <groupId>com.eclipsesource.j2v8</groupId>
  <artifactId>j2v8_macosx_x86_64</artifactId>
  <version>2.0</version>
</dependency></pre>

Hello, World

Once you’ve added the the dependency to your pom.xml, you can begin embedding JavaScript in your Java Application using J2V8. Here is a simple Hello, World! that demonstrates how to call JavaScript from Java and register a callback.

<pre lang="Java">package com.example;
import com.eclipsesource.v8.V8;
public class Hello {
	public void print(String s) {
		System.out.println(s);
	}
	public void start() {
		V8 v8 = V8.createV8Runtime();
		v8.registerJavaMethod(this, "print", "print", 
                                      new Class<?>[] { String.class });
		v8.executeVoidScript("print('Hello, World!');");
	}
	public static void main(String[] args) {
		new Hello().start();
	}
}
</pre>

Screen Shot 2015-02-25 at 9.56.47 AM

More Information

I will presenting J2V8 at EclipseCon in San Fransisco on Thursday March 12th. Stop by and say hi if you’re in the area.

Finally, if you want to try it on Android without embedding it into your own Java application, request an invite to Tabris.js.

6 Comments
  • Don Griffin
    Posted at 06:27, 2015-03-02

    This looks really awesome! Great work and thanks for sharing!

  • Don Griffin
    Posted at 06:32, 2015-03-02

    Any hope of using Chrome Dev Tools (remote) to debug JavaScript code in J2V8?

  • monsterstrike
    Posted at 03:46, 2015-03-03

    cool

  • Tejas
    Posted at 01:32, 2015-03-13

    Is there a way in J2V8 which can be used to make REST calls through Javascript similar to XMLHttpRequest ?

  • Gerry
    Posted at 20:21, 2015-03-20

    I don’t see the build scripts for building the Android V8 engines as part of the git. Are these available elsewhere?