Running JavaScript on GraalVM

less than 1 minute read

GraalVM

GraalVM is a high-performance JDK distribution. It is designed to accelerate the execution of applications written in Java and other JVM languages while also providing runtimes for JavaScript, Ruby, Python, and a number of other popular languages. GraalVM’s polyglot capabilities make it possible to mix multiple programming languages in a single application while eliminating any foreign language call costs.

We are going to see how to run JavaScript inside a Java application using GraalVM.

<dependency>
    <groupId>org.graalvm.sdk</groupId>
    <artifactId>graal-sdk</artifactId>
    <version>21.0.0.2</version>
    <scope>compile</scope>
</dependency>
  • Create a script.js file for our JavaScript code on src/main/resources
"Running " + Graal.language;
  • Create a class containing our main method
package xyz.pretsa.graal;

import java.net.URL;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Source;
import org.graalvm.polyglot.Value;

public class Main {

    public static void main(String[] args) {

        try ( Context context = Context.create()) {
            URL url = Main.class.getResource("/script.js");
            Source script = Source.newBuilder("js", url).build();
            Value value = context.eval(script);
            System.out.println("Result: " + value.asString() + " on " + System.getProperty("java.vendor.version"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
  • Run our application and see the result
Result: Running JavaScript on GraalVM CE 21.0.0.2

Updated:

Comments