6

我在我的项目中添加了 graal 库以在 Java 中执行 JavaScript。我的项目在 spingboot 框架上工作。

compile group: 'org.graalvm.sdk', name: 'graal-sdk', version: '1.0.0-rc9'
compile group: 'org.graalvm.js', name: 'js', version: '1.0.0-rc9'
compile group: 'org.graalvm.js', name: 'js-scriptengine', version: '1.0.0-rc9'
compile group: 'org.graalvm.tools', name: 'profiler', version: '1.0.0-rc9'
compile group: 'org.graalvm.tools', name: 'chromeinspector', version: '1.0.0-rc9'
compile group: 'org.graalvm.truffle', name: 'truffle-api', version: '1.0.0-rc9'
  1. 如果我直接用“gradle bootRun”或IDEA运行我的项目,graal init成功并且工作正常;
  2. 如果我将我的项目构建到一个包含所有库的胖 jar 中,graal init 会失败并抛出 FileSystemNotFoundException。

    java.nio.file.FileSystemNotFoundException: null
    at jdk.zipfs/jdk.nio.zipfs.ZipFileSystemProvider.getFileSystem(ZipFileSystemProvider.java:169)
    at jdk.zipfs/jdk.nio.zipfs.ZipFileSystemProvider.getPath(ZipFileSystemProvider.java:155)
    at java.base/java.nio.file.Paths.get(Paths.java:143)
    at com.oracle.truffle.polyglot.LanguageCache.collectLanguages(LanguageCache.java:284)
    at com.oracle.truffle.polyglot.LanguageCache.createLanguages(LanguageCache.java:211)
    at com.oracle.truffle.polyglot.LanguageCache.languages(LanguageCache.java:201)
    at com.oracle.truffle.polyglot.PolyglotEngineImpl.initializeLanguages(PolyglotEngineImpl.java:480)
    at com.oracle.truffle.polyglot.PolyglotEngineImpl.<init>(PolyglotEngineImpl.java:168)
    at com.oracle.truffle.polyglot.PolyglotEngineImpl.<init>(PolyglotEngineImpl.java:152)
    at com.oracle.truffle.polyglot.PolyglotImpl.buildEngine(PolyglotImpl.java:197)
    at org.graalvm.polyglot.Engine$Builder.build(Engine.java:488)
    at org.graalvm.polyglot.Context$Builder.build(Context.java:1083)
    at org.graalvm.polyglot.Context.create(Context.java:660)
    at ocpm.pcf.policyengine.engine.cache.JavaScriptPolicyParser.graalParse(JavaScriptPolicyParser.java:48)
    

我在异常堆栈中调试了 ponit,发现 graal 想要在 js-1.0.0-rc9.jar 中初始化“lauguage”配置文件。但在上述场景中,它使用了不同的 URI 模式。

 - In the first scenario, the URI is "jar:file:/C:/Users/texu/.gradle/caches/modules-2/files-2.1/org.graalvm.js/js/1.0.0-rc9/25f41dd171dfa5c5921bc1f6bbdf7860279fc43c/js-1.0.0-rc9.jar!/META-INF/truffle/language", which is a normal file path; 
 - In the second scenario, the URI is "jar:file:/app/policy-engine.jar!/BOOT-INF/lib/js-1.0.0-rc9.jar!/META-INF/truffle/language", which is a path of nested jar.

那么,我怎样才能在springboot fat jar中初始化graal?

4

1 回答 1

2

作为一种解决方法,我从springboot 支持的fat jar 中解压 graal 相关的库。然后graal init成功了。在运行时环境中,解压后的库路径如下所示:

/tmp/policy-engine.jar-spring-boot-libs-19affd93-0dd2-451a-b469-75fe0c4f458d/js-scriptengine-1.0.0-rc9.jar
/tmp/policy-engine.jar-spring-boot-libs-19affd93-0dd2-451a-b469-75fe0c4f458d/js-1.0.0-rc9.jar

在我的项目的 build.gradle 中,需要添加以下部分

bootJar {
    requiresUnpack '**/js-*.jar'
    requiresUnpack '**/regex-*.jar'
    requiresUnpack '**/truffle-api-*.jar'
    requiresUnpack '**/graal-sdk-*.jar'
}

更多细节参考: https ://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/gradle-plugin/reference/html/#packaging-executable-configuring-unpacking

于 2018-12-13T08:30:13.027 回答