If you're using GraalVM as your JDK then the GraalJS - GraalVM's Javascript implementation will be available on the classpath automatically and you can use it the with a bunch of import statements as you've used to.
For example the following program would work like below:
import org.graalvm.polyglot.*;
import org.graalvm.polyglot.proxy.*;
public class HelloPolyglot {
public static void main(String[] args) {
System.out.println("Hello Java!");
try (Context context = Context.create()) {
context.eval("js", "print('Hello JavaScript!');");
}
}
}
> javac HelloPolyglot.java
> java HelloPolyglot
Hello Java!
Hello JavaScript!
This is the recommended way to use it, the polyglot API from the GraalVM's sdk, and running on graalvm.
You can also use the ScriptEngine API: https://www.graalvm.org/reference-manual/js/ScriptEngine/
And you also can run it on an arbitrary JDK distribution and use GraalJS as a bunch of dependencies. Here's a sample project you can look at for going this path: https://www.graalvm.org/reference-manual/js/RunOnJDK/, which I would not recommend. GraalVM's JIT is really good at optimizing it at runtime, other JITs aren't that good, so the performance could be worse.
This document can be very helpful when migrating from Nashorn - https://www.graalvm.org/reference-manual/js/ScriptEngine usually there are no complications, but there could be some corner cases around concurrency, etc.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…