34

I'm looking for a solution for embedding the Google JavaScript engine V8 in my Java application.

Have you got some solutions?

Stephan
  • 41,764
  • 65
  • 238
  • 329
  • 9
    You know about [Rhino](http://www.mozilla.org/rhino/), right? If you just want a JavaScript engine for Java and it doesn't *have* to be V8... Rhino compiles JavaScript to Java bytecode, either statically at compile time or dynamically at runtime (or both, if you need both), offering near-full interoperability between code written in Java and code written in JavaScript. It's dead cool, worth looking at if you haven't already. – T.J. Crowder Jun 16 '11 at 09:48
  • I guess you have to study the `Embedder's Guide` : http://code.google.com/apis/v8/embed.html – Talha Ahmed Khan Jun 16 '11 at 09:48
  • 2
    @Thilo V8 is reported to be fast because it's written in C++. – Stephan Jun 16 '11 at 09:50
  • 1
    @Thala It's a C++ developers centric page. I have no skills in C++. – Stephan Jun 16 '11 at 09:52
  • 4
    You will have a hard time embedding v8 without C/C++ skills. Rhino is really well integrated with Java. Is it really too slow? If you are running large JS programs, and speed is of the essence, maybe shelling out to a separate v8 process make sense. – Thilo Jun 16 '11 at 09:55
  • 3
    @Stephan: V8 is indeed freaky-fast compared to most other browser-based JavaScript engines, because it literally compiles JavaScript to machine code on-the-fly. Rhino compiles JavaScript to Java bytecode, either in advance or on-the-fly, which of course your JIT will turn into machine code on-the-fly as and when necessary. If V8 is faster in the end at a given task (and it may be, or may not be), I wonder if it's *enough* of a difference to justify the impedance mis-match and to overcome the boundary-crossings integrating V8 with a JVM will require... – T.J. Crowder Jun 16 '11 at 09:55
  • 2
    @Stephan the speed differences between rhino and v8 shouldn't be that big. Besides your already doing Java, if you care about these kind of speed differences then do C. – Raynos Jun 16 '11 at 09:56
  • This is not about embedded programming. retagged – uɐɪ Jun 16 '11 at 15:43

3 Answers3

27

You can use J2V8 https://github.com/eclipsesource/J2V8. It's even available in Maven Central.

Below is a Hello, World! program using J2V8.

package com.example;

import com.eclipsesource.v8.V8;

public class EclipseCon_snippet5 {


    public static class Printer {
        public void print(String string) {
            System.out.println(string);
        }
    }

    public static void main(String[] args) {
        V8 v8 = V8.createV8Runtime();
        v8.registerJavaMethod(new Printer(), "print", "print", new Class<?>[]{String.class});
        v8.executeVoidScript( "print('Hello, World!');" );
        v8.release(true);
    }

}

You will need to specify your platform in your pom.xml. J2V8 currently supports win32_x86, macosx_x86_64, android_x86 and android_armv7l. The reason they are different is because of the native bindings and pre-build version of V8 that is bundled.

For example, on MacOS you can use.

<dependencies>
    <dependency>
        <groupId>com.eclipsesource.j2v8</groupId>
        <artifactId>j2v8_macosx_x86_64</artifactId>
        <version>2.0</version>
        <scope>compile</scope>
    </dependency>
</dependencies>
Stephan
  • 41,764
  • 65
  • 238
  • 329
irbull
  • 2,490
  • 2
  • 20
  • 28
  • 2
    I'm using v4.2.0 (AAR) on Android (arm + x86) and it works great. It's lightning fast and the API is really cool and well thought. It saves me a lot of work. Otherwise I'd have to port a lot of TypeScript code to Java. Thanks @irbull, you're the man! – hgoebl Jul 03 '16 at 13:28
  • 1
    @hgoebl Thank-you for taking the time to write that. That really made my day! – irbull Jul 27 '16 at 18:31
  • 4
    As of January 2019, @irbull has said that he intends to reduce the scope of J2V8 to support only Android, and only non-NodeJS JavaScript. So if you want a general purpose way to integrate V8 into Java, J2V8 will no longer be it. – Cheeso Mar 19 '19 at 18:14
17

Maybe you could try Jav8, which implement the Java Scripting API (JSR223) base on the Google V8 Javascript engine. I'm working on it from weeks ago, and it could support most simple scenes.

http://code.google.com/p/jav8/

Flier Lu
  • 191
  • 4
6

There's not really any straightforward way you can do it, but, I would suggest Rhino or the JNI. The former is easier, but, not v8, the latter is hard and finicky, but, v8.

Or, you can use a seperate v8 process, and talk with it with Java.

mikhail_b
  • 930
  • 9
  • 10
Dhaivat Pandya
  • 6,499
  • 4
  • 29
  • 43