如何在不使用Maven的情况下使Graal SDK软件包正常工作? [英] How to get Graal SDK packages to work without Maven?

查看:238
本文介绍了如何在不使用Maven的情况下使Graal SDK软件包正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发需要执行的Java应用程序 JavaScript. Nashorn JS引擎即将弃用 替换的是Graal SDK提供的一组API 它利用了GraalVM.本质上是虚拟机 执行多种语言.

I am developing a Java application that needs to execute JavaScript. Nashorn JS engine is about to get deprecated and the replacement is the set of APIs provided by Graal SDK which makes use of GraalVM. The virtual machine that essentially executes a number of languages.

至少我是这样想的. 因此,我花了一天时间尝试使其正常运行.我下载了使用JDK 8的GraalVM rc6.我使用IntelliJ IDEA,并将GraalVM添加为新的JDK.我发现应该将虚拟机添加为开发套件感到很奇怪,但是我知道它基于JDK 8,所以我认为可以.

At least that is what I think. So I spent a day trying to get it working. I downloaded GraalVM rc6 that uses JDK 8. I use IntelliJ IDEA and I added GraalVM as a new JDK. I found it strange that I am supposed to add a virtual machine as a development kit, but I knew it is based on JDK 8 so I was ok with it.

这个问题与其他问题不同,我无法使用新软件包.我的IDE告诉我它无法解决它们.

And the problem was unlike others, I could not get the new packages to work. My IDE told me it cannot resolve them.

实际上,所有org.graalvm软件包都无法解决.

Actually, none of the org.graalvm packages could be resolved.

所以我下载了JDK 11内部版本28,因为据我所知,自版本20开始,GraalVM附带了JDK 11.

So I downloaded JDK 11 build 28 because as far as I know JDK 11 comes with GraalVM since build 20.

实际上,在这种情况下,可以找到一些软件包,但远少于预期:例如org.graalvm.polyglot仍然无法解析.

Actually, in this case, some of the packages could be found, but far less than expected: for instance org.graalvm.polyglot still could not be resolved.

最后,我尝试从Maven添加Graal SDK并解决了软件包.现在运行时出现异常:

Finally, I tried adding Graal SDK from Maven and the packages were resolved. Now I get an exception when running:

Context ctx = Context.create("js");

在类路径上未找到语言和多语言实现.确保truffle-api.jar在类路径上.

No language and polyglot implementation were found on the classpath. Make sure the truffle-api.jar is on the classpath.

我认为前两个案例中的问题实际上不是获得GraalVM JDK,但是即使这样,它也会引发异常.

I thought the problem in the first 2 cases was actually not acquiring GraalVM JDK, but it throws the exception even this way.

有经验的人可以解释这个问题吗?

Can someone experienced explain the matter?

推荐答案

GraalVM

GraalVM是目前高性能的可嵌入多语言虚拟机 支持多种编程语言:Java(和JVM语言), JavaScript(包括node.js),Ruby,R,Python和C/C ++和其他语言 与LLVM后端.

GraalVM

GraalVM is a high-performance embeddable polyglot virtual machine currently supporting a number of programming lanauges: Java (and JVM languages), JavaScript (including node.js), Ruby, R, Python, and C/C++ and other languages with the LLVM backend.

您可以在此处下载GraalVM的预构建发行版: https://www.graalvm.org/downloads . 除此之外,它还包括Java运行时,节点运行时,称为Graal.js的JavaScript引擎等.

You can download the pre-built distribution of GraalVM here: https://www.graalvm.org/downloads. Among other things it includes a java runtime, a node runtime, a JavaScript engine called Graal.js, etc.

Graal SDK 是多语言API,使GraalVM可以使用它可以运行的所有语言实现. 该polyglot API打包为jar文件:$GRAALVM_HOME/jre/lib/boot/graal-sdk.jar.

Graal SDK is the polyglot API allowing GraalVM to work with all the language implementations it can run. This polyglot API is packaged as a jar file: $GRAALVM_HOME/jre/lib/boot/graal-sdk.jar.

将该文件作为外部库添加到您的IDEA项目/模块中,将使IDE可以找到以下类: 与语言互操作所必需的org.graalvm.polyglot.Contextorg.graalvm.polyglot.Value JavaScript实现.

Adding that file as an external library to your IDEA project / module, would allow the IDE to find the classes like: org.graalvm.polyglot.Context and org.graalvm.polyglot.Value which are necessary to interop with the languages, including the JavaScript implementation.

如果您的项目使用的是Maven,则可以在该文件上添加系统依赖项,并且maven可以在以下任何系统上找到它: $JAVA_HOME设置为指向GraalVM发行版.

If your project is using Maven, you can add a system dependency on that file, and maven will find it on any system where $JAVA_HOME is set to point at a GraalVM distribution.

<dependency>
    <groupId>org.graalvm</groupId>
    <artifactId>graal-sdk</artifactId>
    <version>1.0.0-rc</version>
    <scope>system</scope>
    <systemPath>${java.home}/lib/boot/graal-sdk.jar</systemPath>
</dependency>

现在,当您从GraalVM发行版中运行java命令时,所需的文件将自动添加到类路径中. 因此,在IDE中运行类似以下内容的东西就没有必要了:

Now when you will run the java command from the GraalVM distribution, the necessary files will be added to the classpath automatically. So nothing more is necessary to run something like the following in the IDE:

import org.graalvm.polyglot.*;
public class Main {
    public static void main(String[] args) {
        Context polyglot = Context.create();
        Value array = polyglot.eval("js", "[1,2,42,4]");
        System.out.println(array.getArrayElement(2).asInt());
    }
}

现在这是因为GraalVM默认情况下启用了Graal.js JavaScript引擎.

Now this is because GraalVM has the Graal.js JavaScript engine enabled by default.

如果要在库存JDK上运行它,则需要在类路径中添加更多内容.

If you want to run it on a stock JDK you need to add more things to the classpath.

有一个关于如何在股票JDK上运行Graal.js的问题:

There's this question on how to run Graal.js on the stock JDK: How to use graaljs ? Is there a place where to get a .jar file/files?. The accepted answer tells in more details where to find the necessary jar files to make it work on Java 8.

简而言之,您需要将以下jar文件添加到类路径以使其实际运行:

In a nutshell you need to add the following jar files to the classpath to make it actually run:

  • graal-sdk.jar- GraalVM多语言API
  • truffle-api.jar-用于语言实现的API.
  • graaljs.jar -这是GraalVM的JavaScript引擎的实现
  • graaljs-scriptengine.jar-允许通过Java脚本引擎API使用Graal.js.
  • graaljs-launcher.jar
  • tregex.jar-正则表达式库
  • truffle-profiler.jar-松露语言实现的探查器
  • chromeinspector.jar-调试器集成
  • launcher-common.jar
  • graal-sdk.jar - GraalVM polyglot API
  • truffle-api.jar - API for language implementations.
  • graaljs.jar - this is the implementation of GraalVM's JavaScript Engine
  • graaljs-scriptengine.jar -- allows to use Graal.js through the Java script engine API.
  • graaljs-launcher.jar
  • tregex.jar -- regexp library
  • truffle-profiler.jar - profiler for Truffle languages implementations
  • chromeinspector.jar - debugger integration
  • launcher-common.jar

您可以在您下载的的GraalVM发行版中找到它们,两个版本都可以正常工作.

You can find them in the GraalVM distribution that you downloaded, both editions would work fine.

现在,如果没有Graal编译器,JavaScript引擎的性能将不是最佳的. 正如您自己提到的那样,JDK 11附带了Graal 编译器(不是GraalVM)的快照,它是GraalVM项目的完整版本,包括JS引擎,LLVM位代码解释器,节点实现,JVM等.您可以通过以下方式启用Graal编译器: --XX:+UnlockExperimentalVMOptions --XX:+UseJVMCICompilerjava命令.

Now without the Graal compiler the performance of the JavaScript engine would not be optimal. As you mentioned yourself JDK 11 comes with a snapshot of the Graal compiler (not GraalVM, which is a full distribtuion of the GraalVM project including JS enginer, LLVM bitcode interpreter, node implementation, JVM, etc). You can enable the Graal compiler by passing --XX:+UnlockExperimentalVMOptions --XX:+UseJVMCICompiler to the java command.

现在在JDK 11上全部运行可能,因为JDK 11与JDK 8完全不同,并且模块系统可能存在问题或缺少某些东西(如jax-b),因此无法运行也可能有效.它将在JDK 8上运行.

Now running it all on JDK 11 might not work because JDK 11 is sufficiently different from JDK 8 and there could be problems with the module system or things missing (like jax-b), but it also might work. It would work on JDK 8.

这篇关于如何在不使用Maven的情况下使Graal SDK软件包正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆