转载

使用graaljs引擎执行js代码

添加依赖

        <dependency>
            <groupId>org.graalvm.js</groupId>
            <artifactId>js</artifactId>
            <version>${graal.version}</version>
        </dependency>
        <dependency>
            <groupId>org.graalvm.js</groupId>
            <artifactId>js-scriptengine</artifactId>
            <version>${graal.version}</version>
        </dependency>

helloworld

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class JsEngineHelloWorld {

    public static void main(String[] args) throws ScriptException {
        ScriptEngine engine = new ScriptEngineManager().getEngineByName("graal.js");
        engine.eval("print('Hello World!');");
    }

}
运行,输出
Hello World!
链接:https://www.jianshu.com/p/f116c80ce6a1
正文到此结束
Loading...