How to get Graal SDK packages to work without Maven?

后端 未结 5 1052
夕颜
夕颜 2021-02-06 04:55

I am developing a Java application that needs to execute JavaScript. Nashorn JS engine is about to get deprecated and the replacement is the set of APIs provided by Graal SDK wh

5条回答
  •  日久生厌
    2021-02-06 05:20

    GraalVM

    GraalVM is a high-performance embeddable polyglot virtual machine currently supporting a number of programming lanauges: Java (and JVM languages), JavaScript (including node.js), Ruby, R, Python, and C/C++ and other languages with the LLVM backend.

    You can download the pre-built distribution of GraalVM here: https://www.graalvm.org/downloads. Among other things it includes a java runtime, a node runtime, a JavaScript engine called Graal.js, etc.

    Graal SDK is the polyglot API allowing GraalVM to work with all the language implementations it can run. This polyglot API is packaged as a jar file: $GRAALVM_HOME/jre/lib/boot/graal-sdk.jar.

    Adding that file as an external library to your IDEA project / module, would allow the IDE to find the classes like: org.graalvm.polyglot.Context and org.graalvm.polyglot.Value which are necessary to interop with the languages, including the JavaScript implementation.

    If your project is using Maven, you can add a system dependency on that file, and maven will find it on any system where $JAVA_HOME is set to point at a GraalVM distribution.

    
        org.graalvm
        graal-sdk
        1.0.0-rc
        system
        ${java.home}/lib/boot/graal-sdk.jar
    
    

    Now when you will run the java command from the GraalVM distribution, the necessary files will be added to the classpath automatically. So nothing more is necessary to run something like the following in the IDE:

    import org.graalvm.polyglot.*;
    public class Main {
        public static void main(String[] args) {
            Context polyglot = Context.create();
            Value array = polyglot.eval("js", "[1,2,42,4]");
            System.out.println(array.getArrayElement(2).asInt());
        }
    }
    

    Now this is because GraalVM has the Graal.js JavaScript engine enabled by default.

    If you want to run it on a stock JDK you need to add more things to the classpath.

    Running Graal.js on a stock JDK**

    There's this question on how to run Graal.js on the stock JDK: How to use graaljs ? Is there a place where to get a .jar file/files?. The accepted answer tells in more details where to find the necessary jar files to make it work on Java 8.

    In a nutshell you need to add the following jar files to the classpath to make it actually run:

    • graal-sdk.jar - GraalVM polyglot API
    • truffle-api.jar - API for language implementations.
    • graaljs.jar - this is the implementation of GraalVM's JavaScript Engine
    • graaljs-scriptengine.jar -- allows to use Graal.js through the Java script engine API.
    • graaljs-launcher.jar
    • tregex.jar -- regexp library
    • truffle-profiler.jar - profiler for Truffle languages implementations
    • chromeinspector.jar - debugger integration
    • launcher-common.jar

    You can find them in the GraalVM distribution that you downloaded, both editions would work fine.

    Now without the Graal compiler the performance of the JavaScript engine would not be optimal. As you mentioned yourself JDK 11 comes with a snapshot of the Graal compiler (not GraalVM, which is a full distribtuion of the GraalVM project including JS enginer, LLVM bitcode interpreter, node implementation, JVM, etc). You can enable the Graal compiler by passing --XX:+UnlockExperimentalVMOptions --XX:+UseJVMCICompiler to the java command.

    Now running it all on JDK 11 might not work because JDK 11 is sufficiently different from JDK 8 and there could be problems with the module system or things missing (like jax-b), but it also might work. It would work on JDK 8.

提交回复
热议问题