how to run javascript inside existing grails or spring application using GraalVM?

John Little :

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?

BoriS :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=327024&siteId=1