gpt4 book ai didi

java - 类型错误 : invokeMember GraalVM native-image

转载 作者:行者123 更新时间:2023-12-01 16:35:05 25 4
gpt4 key购买 nike

我正在尝试使用 GraalVM 创建 native 镜像,我的代码:

import org.graalvm.polyglot.HostAccess;

public class Console {

@HostAccess.Export
public void print(String msg){
System.out.println(msg);
}
}

然后我这样称呼代码本身:

public void callMethod(CommonRequest request) throws ScriptException, IOException, NoSuchMethodException {
StringBuilder s = new StringBuilder();

s.append(new PluginJS().load(request.getMethodPath(), true));

Context context = null;
try {
context = Context.newBuilder()
.allowHostAccess(HostAccess.ALL)
.allowAllAccess(true)
.allowCreateThread(true)
.allowHostClassLoading(true)
.allowIO(true)
.allowNativeAccess(true)
.allowCreateProcess(true).build();

putMembers(context.getBindings("js"));

context.eval("js", s.toString());

} catch (Exception e) {
e.printStackTrace();

} finally {
context.close();

}

}

private void putMembers(Value value) {
value.putMember("Console", new Console());


}

当我通过 IDE 或 java -jar 运行时它工作正常,但是当我尝试编译为 native 镜像时会抛出错误。下面的错误后面是用于编译 native 镜像的命令行。

错误:

类型错误:JavaObject[com.compiler.commons.log.Console@113a2d320 (com.com.compiler.commons.log.Console)] 上的 invokeMember(打印)失败,原因是:未知标识符:打印

命令行

graalvm-ce-java8-20.0.0/Contents/Home/bin/java -jar -agentlib:native-image-agent=config-merge-dir=/Users/ze/Documents/java/tool/config compiler-1.0-SNAPSHOT-jar-with-dependencies.jar

graalvm-ce-java8-20.0.0/Contents/Home/bin/native-image --language:js --initialize-at-build-time nomeApp -jar compiler-1.0-SNAPSHOT-jar-with-dependencies.jar

请问有人可以帮助我吗?非常感谢

最佳答案

我已经解决了,我需要从“/config”文件中清理,沿着代理执行应用程序,然后编译添加一些不同的参数。请参阅下面的内容:

代理

/Users/ze/Documents/programs/graalvm-ce-java8-20.0.0/Contents/Home/bin/java -jar -agentlib:native-image-agent=config-merge-dir=/Users/ze/Documents/gitprojects/java/tool/config ./target/compiler-1.0-SNAPSHOT-jar-with-dependencies.jar 

编译

sudo /Users/ze/Documents/programs/graalvm-ce-java8-20.0.0/Contents/Home/bin/native-image --language:js --initialize-at-build-time -H:+AllowIncompleteClasspath -H:+ReportExceptionStackTraces --report-unsupported-elements-at-runtime -H:ConfigurationFileDirectories=/Users/ze/Documents/gitprojects/java/tool/config nameOfApp -jar ./target/compiler-1.0-SNAPSHOT-jar-with-dependencies.jar

关于java - 类型错误 : invokeMember GraalVM native-image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61970674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com