如何在 Java 应用程序中嵌入 V8?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6369919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to embed V8 in a Java application?
提问by Stephan
I'm looking for a solution for embedding the Google JavaScript engine V8 in my Java application.
我正在寻找将 Google JavaScript 引擎 V8 嵌入到我的 Java 应用程序中的解决方案。
Have you got some solutions?
你有一些解决办法吗?
采纳答案by irbull
You can use J2V8 https://github.com/eclipsesource/J2V8. It's even available in Maven Central.
您可以使用 J2V8 https://github.com/eclipsesource/J2V8。它甚至可以在Maven Central 中使用。
Below is a Hello, World! program using J2V8.
下面是你好,世界!使用 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.
您需要在 pom.xml 中指定您的平台。J2V8 目前支持 win32_x86、macosx_x86_64、android_x86 和 android_armv7l。它们不同的原因是因为捆绑了本机绑定和 V8 的预构建版本。
For example, on MacOS you can use.
例如,在 MacOS 上您可以使用。
<dependencies>
<dependency>
<groupId>com.eclipsesource.j2v8</groupId>
<artifactId>j2v8_macosx_x86_64</artifactId>
<version>2.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
回答by Dhaivat Pandya
There's not really any straightforward way you can do it, but, I would suggest Rhinoor the JNI. The former is easier, but, not v8, the latter is hard and finicky, but, v8.
没有任何直接的方法可以做到,但是,我建议使用Rhino或JNI。前者更容易,但不是 v8,后者很难和挑剔,但是,v8。
Or, you can use a seperate v8 process, and talk with it with Java.
或者,您可以使用单独的 v8 进程,并使用 Java 与之对话。
回答by Flier Lu
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.
也许您可以尝试 Java 8,它实现了基于 Google V8 Javascript 引擎的 Java Scripting API (JSR223)。我从几周前就开始研究它,它可以支持大多数简单的场景。