GraalVM Polyglot API

Jakob Jenkov
Last update: 2022-01-01

The GraalVM Polyglot API enables your applications running in GraalVM to execute code in multiple other languages from within your application. For instance, from a Java application you can execute JavaScript code.

If you use GraalVM as your Java SDK - then you already have the Polyglot API available on the classpath. There is nothing else to add.

Why Polyglot Applications?

An application or system developed using multiple different languages is often referred to as a polyglot application or system. Polyglot applications are becoming more and more common these days - but you might still ask yourself when executing multiple languages within the same app makes sense?

Imagine you have a Java application, and you need to execute some CPU-intensive encryption, compression or data science algorithm for which you do not have a hardware-accelerated API already (AES encryption in Java uses hardware acceleration underneath when available in the underlying hardware). In these situations you could implement the encryption, compression or data science algorithm in C / C++ instead, compile it into LLVM and include that LLVM in your Java app. That should enable your app to have a higher performance than if implemented in Java (in theory at least - in practice, measure what actually runs fastest).

You could also be in a situation in which you only have an algorithm implementation in one language (e.g. Python), and do not have competencies in your team to translate it to your chosen programming language, or maybe you don't have the time, or maybe you just want to test if that algorithm could solve your problem - before translating it to another language.

Personally, I prefer to keep an app in as few languages as possible, preferably only one language, but as stated above, there are corner cases where calling from one language to another makes sense.

GraalVM Polyglot Java Example

Here is a simple example of a Java application that executes a simple JavaScript expression via the GraalVM Polyglot API:

import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Value;

public class PolyglotExample {
    public static void main(String[] args) {

        // create a JavaScript expression which is evaluated immediately.

        try(Context context = Context.create()) {

            Value expression = context.eval("js", "2 + 3");

            System.out.println(expression.asInt());
        }
    }
}

Here is a similar example where a Java application define and call a JavaScript function via the GraalVM Polyglot API:

import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Value;

public class PolyglotExample {
    public static void main(String[] args) {

        // create a JavaScript function which can be called multiple times.

        try(Context context = Context.create()) {

            Value function = context.eval("js",
                "(function sum(p1, p2){ return p1 + p2; } )");

            Value result1 = function.execute(4, 5);
            System.out.println(result1.asInt());

            Value result2 = function.execute(6, 1);
            System.out.println(result2.asInt());
        }
    }
}

Jakob Jenkov

Featured Videos

Java Generics

Java ForkJoinPool

P2P Networks Introduction



















Close TOC
All Tutorial Trails
All Trails
Table of contents (TOC) for this tutorial trail
Trail TOC
Table of contents (TOC) for this tutorial
Page TOC
Previous tutorial in this tutorial trail
Previous
Next tutorial in this tutorial trail
Next