问题
I have project used Nashorn Javascript engine. I'm trying to migrate to java11 and also migrate from Nashorn to Graal. I've read here that I can use graal via the standard JDK installation starting from JDK 11. Also I've read there that Graal-SDK are uploaded to Maven central, and that there is Java flag polyglot.js.nashorn-compat
for easy migration. So I've used jdk11, add maven dependency to pom.xml and used java flag but when I'm trying to get engine by name "graal.js", I've got null here:
ScriptEngine engine = engineManager.getEngineByName("graal.js")
What I'm missing? How to make it work?
回答1:
Here is a sample maven project that shows how to run the GraalVM JavaScript engine on JDK11 both through the scripting API and the polyglot API. Hope it helps!
https://github.com/graalvm/graal-js-jdk11-maven-demo
The gist of it is to add the necessary dependencies (graal-sdk, js, js-scriptengine, and optionally profiler and chromeinspector), Run with enabled experimental options and the JVMCI compiler (-XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
) and upgrade the module path with the graal jar (--upgrade-module-path=${compiler.dir}/compiler.jar
) which is also available from maven (org.graalvm.compiler:compiler
).
回答2:
You are missing the following dependencies:
<dependency>
<groupId>org.graalvm.js</groupId>
<artifactId>js-scriptengine</artifactId>
</dependency>
<dependency>
<groupId>org.graalvm.truffle</groupId>
<artifactId>truffle-api</artifactId>
</dependency>
js-scriptengine
contains the ScriptEngine
implementation: com.oracle.truffle.js.scriptengine.GraalJSScriptEngine
.
And the truffle-api is required (you only get the rror message if you instanciate the GraalJSEngineFactory
directly:
GraalJSEngineFactory gsf = new GraalJSEngineFactory();
However there seems to be another package missing, as it does not work for me.
来源:https://stackoverflow.com/questions/52932679/use-graalvm-via-the-standard-jdk-11