5

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?

Robert
  • 39,162
  • 17
  • 99
  • 152
Valentina Chumak
  • 279
  • 6
  • 17
  • 2
    how about [trying the other names](https://github.com/graalvm/graaljs/blob/master/docs/user/NashornMigrationGuide.md#scriptengine-name-graaljs) like *"JavaScript", "js"*? – Naman Oct 22 '18 at 15:26
  • 4
    You've misunderstood, Graalvm is not a part of JDK 11, but it can run on the standard JDK 11 (as opposed to the oracle labs jdk 8). GraalVM is it's own virtual machine, separate from Hotspot: https://www.graalvm.org/ – Jorn Vernee Oct 22 '18 at 15:29
  • @JornVernee could you explain for me what does it mean "run graalvm on standard jdk"? – Valentina Chumak Oct 22 '18 at 16:55
  • @nullpointer In those cases I got "jdk.nashorn.api.scripting.NashornScriptEngine" which is deprecated – Valentina Chumak Oct 22 '18 at 16:56
  • It means that GraalVM uses the standard Java APIs from that JDK. – Jorn Vernee Oct 22 '18 at 17:17
  • 1
    The Graal SDK does not contain the `ScriptEngine`implementation. You need the `graaljs-scriptengine.jar`. This is also mention here: https://stackoverflow.com/a/50742571/150978 – Robert Oct 22 '18 at 18:35
  • @JornVernee ok, I've downloaded graalvm, run my code this way and it work: "./graalvm-ce-1.0.0-rc8/bin/java -Dpolyglot.js.nashorn-compat=true MyTest" But java version is java8. how can it can be used with java11? and if there is ScriptEngine API (JSR 223) as part of java specification, what should I use with openjdk if Nashorn is deprecated? – Valentina Chumak Oct 23 '18 at 08:45
  • 1
    I think they're only shipping releases of GraalVM built on top of JDK 8, you could ask on the github how to get a JDK 11 build of GraalVM. As for a replacement for Nashorn in OpenJDK, afaik there won't be one, but deprecated does not mean that it will be removed right away, just that it won't get updated anymore. – Jorn Vernee Oct 23 '18 at 10:30

2 Answers2

6

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).

BoriS
  • 907
  • 5
  • 13
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.

Robert
  • 39,162
  • 17
  • 99
  • 152