我正在尝试在项目中使用GraalVM添加简单的脚本功能。我正在使用Maven进行依赖管理,以加载Graal的基本依赖。这是我的pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cx.matthew</groupId>
<artifactId>graaltest</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<graalvm.version>19.0.2</graalvm.version>
<compiler.dir>${project.build.directory}/compiler</compiler.dir>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Maven Repository Switchboard</name>
<url>http://repo1.maven.org/maven2</url>
</repository>
<repository>
<id>papermc</id>
<url>https://papermc.io/repo/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.graalvm.sdk</groupId>
<artifactId>graal-sdk</artifactId>
<version>${graalvm.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.graalvm.js</groupId>
<artifactId>js</artifactId>
<version>${graalvm.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.graalvm.js</groupId>
<artifactId>js-scriptengine</artifactId>
<version>${graalvm.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.graalvm.tools</groupId>
<artifactId>profiler</artifactId>
<version>${graalvm.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.graalvm.tools</groupId>
<artifactId>chromeinspector</artifactId>
<version>${graalvm.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.graalvm.truffle</groupId>
<artifactId>truffle-api</artifactId>
<version>19.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.destroystokyo.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.14.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
这是我调用Graal的代码:
package cx.matthew.graaltest;
import org.bukkit.plugin.java.JavaPlugin;
import org.graalvm.polyglot.Context;
public class GraalTestPlugin extends JavaPlugin {
@Override
public void onEnable() {
Context context = Context.create();
context.eval("js", "console.log(\"hi!!!\");");
}
}
尽管据我所知,它应该可以运行,但运行时会输出以下错误:
[19:25:16 ERROR]: Error occurred while enabling GraalTest v1.0-SNAPSHOT (Is it up to date?)
java.lang.IllegalStateException: No language and polyglot implementation was found on the classpath. Make sure the truffle-api.jar is on the classpath.
at org.graalvm.polyglot.Engine$PolyglotInvalid.noPolyglotImplementationFound(Engine.java:800) ~[?:?]
at org.graalvm.polyglot.Engine$PolyglotInvalid.buildEngine(Engine.java:732) ~[?:?]
at org.graalvm.polyglot.Engine$Builder.build(Engine.java:505) ~[?:?]
at org.graalvm.polyglot.Context$Builder.build(Context.java:1304) ~[?:?]
at org.graalvm.polyglot.Context.create(Context.java:697) ~[?:?]
at cx.matthew.graaltest.GraalTestPlugin.onEnable(GraalTestPlugin.java:10) ~[?:?]
at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:263) ~[patched_1.14.2.jar:git-Paper-97]
at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:338) ~[patched_1.14.2.jar:git-Paper-97]
at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:419) ~[patched_1.14.2.jar:git-Paper-97]
at org.bukkit.craftbukkit.v1_14_R1.CraftServer.enablePlugin(CraftServer.java:464) ~[patched_1.14.2.jar:git-Paper-97]
at org.bukkit.craftbukkit.v1_14_R1.CraftServer.enablePlugins(CraftServer.java:378) ~[patched_1.14.2.jar:git-Paper-97]
at org.bukkit.craftbukkit.v1_14_R1.CraftServer.reload(CraftServer.java:854) ~[patched_1.14.2.jar:git-Paper-97]
at org.bukkit.Bukkit.reload(Bukkit.java:610) ~[patched_1.14.2.jar:git-Paper-97]
at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:54) ~[patched_1.14.2.jar:git-Paper-97]
at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:159) ~[patched_1.14.2.jar:git-Paper-97]
at org.bukkit.craftbukkit.v1_14_R1.CraftServer.dispatchCommand(CraftServer.java:736) ~[patched_1.14.2.jar:git-Paper-97]
at org.bukkit.craftbukkit.v1_14_R1.CraftServer.dispatchServerCommand(CraftServer.java:698) ~[patched_1.14.2.jar:git-Paper-97]
at net.minecraft.server.v1_14_R1.DedicatedServer.handleCommandQueue(DedicatedServer.java:457) ~[patched_1.14.2.jar:git-Paper-97]
at net.minecraft.server.v1_14_R1.DedicatedServer.b(DedicatedServer.java:419) ~[patched_1.14.2.jar:git-Paper-97]
at net.minecraft.server.v1_14_R1.MinecraftServer.a(MinecraftServer.java:1061) ~[patched_1.14.2.jar:git-Paper-97]
at net.minecraft.server.v1_14_R1.MinecraftServer.run(MinecraftServer.java:905) ~[patched_1.14.2.jar:git-Paper-97]
at java.lang.Thread.run(Thread.java:834) [?:?]
现在,当然,针对此错误的建议解决方案是在类路径中包含truffle-api.jar,但是由于此操作在插件环境中运行,让此插件的最终用户不理想必须手动设置其类路径。 truffle-api Maven依赖项中的阴影似乎不起作用,这通常是解决此类问题的方法。
已经提出了一些现有的解决方案。我遇到的第一个是在this answer中,但是正如您在pom.xml中所看到的那样,所有这些依赖项都已包括在内。
我见过的另一种解决方案(以及问题)在this GitHub issue中进行了概述,但是尽管我可以确认修改后的META-INF / truffle / language文件在我的最终jar中,但它并没有似乎在工作。这是jar中输出的文件:
language1.characterMimeType.0=application/tregex
language1.className=com.oracle.truffle.regex.RegexLanguage
language1.id=regex
language1.implementationName=
language1.interactive=false
language1.internal=true
language1.name=REGEX
language1.version=0.1
language2.characterMimeType.0=application/javascript
language2.characterMimeType.1=application/javascript+module
language2.characterMimeType.2=text/javascript
language2.className=com.oracle.truffle.js.lang.JavaScriptLanguage
language2.defaultMimeType=application/javascript
language2.dependentLanguage.0=regex
language2.fileTypeDetector0=com.oracle.truffle.js.lang.JSFileTypeDetector
language2.id=js
language2.implementationName=GraalVM JavaScript
language2.interactive=true
language2.internal=false
language2.name=JavaScript
language2.version=inherit
所以,我有点不知所措。是否有人对如何解决此问题有任何想法?
答案 0 :(得分:0)
到目前为止,我发现解决此问题的最佳方法是将上下文ClassLoader换出正确的内容,然后再将其换回:
ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
// Insert Graal code here
Thread.currentThread().setContextClassLoader(oldCl);
您甚至可以将其设置为回调,因此您不必一遍又一遍地使用相同的代码:
private void runInPluginContext(ContextCallback callback) {
ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
callback.call();
Thread.currentThread().setContextClassLoader(oldCl);
}
interface ContextCallback {
void call();
}
并使用以下命令运行它:
runInPluginContext(() -> {
// Insert Graal code here
});
这显然不是理想的,因为它要求您每次调用Graal代码时都要更改上下文。如果有人有更好的解决方案,我很乐意看到它!