问题描述
我正在寻找在我的Java应用程序中嵌入Google JavaScript引擎V8的解决方案。
I'm looking for a solution for embedding the Google JavaScript engine V8 in my Java application.
您有解决方案吗?
推荐答案
您可以使用J2V8 。它甚至可以在中找到。
You can use J2V8 https://github.com/eclipsesource/J2V8. It's even available in Maven Central.
以下是Hello,World!程序使用J2V8。
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);
}
}
您需要指定您的平台在你的pom.xml中。 J2V8目前支持win32_x86,macosx_x86_64,android_x86和android_armv7l。它们不同的原因是由于捆绑的V8的原生绑定和预构建版本。
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.
例如,在MacOS上你可以使用。
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>
这篇关于如何在Java应用程序中嵌入V8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!