How to embed V8 in a Java application?
Asked Answered
T

4

34

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

Have you got some solutions?

Treytri answered 16/6, 2011 at 9:44 Comment(8)
You know about 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.Rain
I guess you have to study the Embedder's Guide : code.google.com/apis/v8/embed.htmlIsaac
@Thilo V8 is reported to be fast because it's written in C++.Treytri
@Thala It's a C++ developers centric page. I have no skills in C++.Treytri
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.Sound
@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...Rain
@Treytri 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.Headreach
This is not about embedded programming. retaggedRascality
M
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>
Metrics answered 6/3, 2015 at 18:31 Comment(3)
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!Wasteful
@Wasteful Thank-you for taking the time to write that. That really made my day!Metrics
As of January 2019, @Metrics 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.Eyeful
U
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/

Untidy answered 27/7, 2011 at 16:17 Comment(2)
The link is dead.Hoar
The link is not dead in fact.Extinctive
H
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.

Himyarite answered 16/6, 2011 at 9:47 Comment(0)
W
0

For the new visitors, I would say the best solution right now is Javet. It supports the newest V8 and Node.js engines.

Wallache answered 24/4 at 9:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.