简体   繁体   English

如何使用GraalVM在现有的grails或spring应用程序中运行javascript?

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

Nashorn has been deprecated unfortunately, as there were lots of examples. 不幸的是,Nashorn已被弃用,因为有很多示例。

We understand that GraalVM can compile and run applications. 我们了解GraalVM可以编译和运行应用程序。 We dont need this. 我们不需要这个。

We just want to run some javascript in our java apps. 我们只想在我们的Java应用程序中运行一些javascript。 Load a javascript file, then call methods on it periodically, and have javascript call java in response. 加载一个javascript文件,然后定期调用其上的方法,然后让javascript调用java作为响应。 The javascript is an engine we need to be able to run in the browser client or on the server. javascript是我们需要能够在浏览器客户端或服务器上运行的引擎。

Has anyone seen a way to use GraalVM to run js files in a spring app, or better, grails? 有没有人看到过使用GraalVM在spring app或更好的grails中运行js文件的方法? We use gradle. 我们使用gradle。

To be clear, we dont want to use graalvm to compile our application. 需要明确的是,我们不想使用graalvm来编译我们的应用程序。 We only want a utility which can run js inside our existing applications. 我们只需要一个可以在现有应用程序中运行js的实用程序。

Examples and documentation are thin on the ground. 示例和文档很少。 There don't seem to be any gradle plugins or options to support it either. 似乎也没有任何gradle插件或选项来支持它。

Or should we stick with Nashorn? 还是我们应该坚持使用Nashorn?

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);
  }
}

The recommended way of running the GraalVM JavaScript engine is unsurprisingly GraalVM. 毫无疑问,运行GraalVM JavaScript引擎的推荐方法是GraalVM。 There seems to be some misunderstanding on your part between GraalVM and GraalVM native-image. 在GraalVM和GraalVM本机映像之间,您似乎有一些误解。 GraalVM is a fully featured JDK8 that does support ahead of time compilation of java applications, but it does not mandate it. GraalVM是功能齐全的JDK8,它确实支持 Java应用程序的提前编译,但没有强制要求 You can use GraalVM as you would use any other JDK8 (eg point JAVA_HOME to the GraalVM dir). 您可以像使用任何其他JDK8一样使用GraalVM(例如,将JAVA_HOME指向GraalVM目录)。 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 ) 如果您正在运行JDK11 +,请查看示例maven项目https://github.com/graalvm/graal-js-jdk11-maven-demo (如此处所回答, 通过标准JDK 11使用graalvm

If you are running on JDK8 and insist on not using GraalVM your options are rather limited. 如果您在JDK8上运行并且坚持不使用GraalVM,那么您的选择将非常有限。 You can still use the GraalVM javascript engine but in interpreter mode only resulting in worse performance. 您仍然可以使用GraalVM javascript引擎,但是在解释器模式下只会导致性能下降。 The example repository I linked to has a "jdk8" profile in the pom.xml file which show how to run on stock jdk8. 我链接到的示例存储库在pom.xml文件中有一个“ jdk8”配置文件,该配置文件显示了如何在库存jdk8上运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 GraalVM JDK 构建/运行您的应用程序 - Build / Run your application using GraalVM JDK 将grails集成到现有的spring应用程序中? - Integrating grails into an existing spring application? 无法使用 GraalVM 运行 SpringBoot - 使用 Docker 容器的 Postgres 应用程序 - Unable to run with GraalVM a SpringBoot - Postgres application using Docker containers 使用GraalVM在Heroku上运行Java - Using GraalVM to run Java on Heroku 尝试运行 Java 17 时出错 - Spring Boot 3 项目与 ehcache 使用 GraalVM 本机映像 - Error when trying to run Java 17 - Spring Boot 3 project with ehcache using GraalVM native image 使用 GraalVM 在 AWS Lambda 中运行 Java - Using GraalVM to run Java in AWS Lambda 在现有的带有JSP的Spring中使用Spring MVC - Using Spring MVC in an existing Spring with JSP application Java 中的 GraalVM JavaScript - 如何识别异步方法 - GraalVM JavaScript in Java - How to identify an async method Java Spring 启动应用程序中的 Graalvm 多语言线程问题 - Graalvm Polyglot Thread issue in Java Spring boot application 使用GraalVM在Java中读取/加载Javascript时如何退出无限JS执行循环? - How to exit infinite JS execution loop when reading/loading Javascript in Java using GraalVM?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM