4

Nashorn has been deprecated unfortunately, as there were lots of examples.

We understand that GraalVM can compile and run applications. We dont need this.

We just want to run some javascript in our java apps. Load a javascript file, then call methods on it periodically, and have javascript call java in response. The javascript is an engine we need to be able to run in the browser client or on the server.

Has anyone seen a way to use GraalVM to run js files in a spring app, or better, grails? We use gradle.

To be clear, we dont want to use graalvm to compile our application. We only want a utility which can run js inside our existing applications.

Examples and documentation are thin on the ground. There don't seem to be any gradle plugins or options to support it either.

Or should we stick with Nashorn?

John Little
  • 10,707
  • 19
  • 86
  • 158

2 Answers2

1

According to the documentation, it should go like that:

package graaltest;
import org.graalvm.polyglot.*;
public class GraalVMTest {
  public static void main(String[] args) {
    Context context = Context.create();
    context.eval("js", "var test = Java.type('graaltest.GraalVMTest'); test.hello('World');");
  }

  public static void hello(String what) {
    System.out.println("Hello "+what);
  }
}
Erich Kitzmueller
  • 36,381
  • 5
  • 80
  • 102
  • Thanks, but the crux is how to get the required libraries into the app, and how to then compile it so we can run it. We have existing grails apps for example we would like to use js in. Grails uses gradle. There don't seem to be any examples of using gradle (or even maven) to include whatevers needed into the project. Perhaps this cant be done? Nashorn is easy - its built into java 8 – John Little Sep 17 '19 at 09:00
1

The recommended way of running the GraalVM JavaScript engine is unsurprisingly GraalVM. There seems to be some misunderstanding on your part between GraalVM and GraalVM native-image. GraalVM is a fully featured JDK8 that does support ahead of time compilation of java applications, but it does not mandate it. You can use GraalVM as you would use any other JDK8 (e.g. point JAVA_HOME to the GraalVM dir). You just get polyglot support and likely better performance.

If you are running JDK11+ have a look at the example maven project https://github.com/graalvm/graal-js-jdk11-maven-demo (as answered here Use graalvm via the standard JDK 11)

If you are running on JDK8 and insist on not using GraalVM your options are rather limited. You can still use the GraalVM javascript engine but in interpreter mode only resulting in worse performance. The example repository I linked to has a "jdk8" profile in the pom.xml file which show how to run on stock jdk8.

BoriS
  • 907
  • 5
  • 13
  • Very interesting! Having read a lot about graalvm, I was missing the critical information that it is a replacement for JDK. However, if it is a replacement for JDK8, why is it not also a replacement for JDK11? We have been running oracles Java 8 for years in production - there would be some concern about swapping for a new and unknown VM. – John Little Sep 17 '19 at 10:34
  • Just looked around. Seems graalVM is not widely used yet (as a JDK replacement). https://technology.amis.nl/2018/11/23/comparing-jvm-performance-zulu-openjdk-openjdk-oracle-jdk-graalvm-ce/#prettyPhoto This site shows it uses more CPU and has lower response times, but this could be different now. – John Little Sep 17 '19 at 10:46
  • A JDK11 based GraalVM is in the works. GraalVM might be new but it's far from unknown (especially if you already run Oracle https://www.oracle.com/tools/graalvm-enterprise-edition.html). I'd recommend you try it out yourself and see how it performs for your particular workload. – BoriS Sep 17 '19 at 12:04