【问题标题】:TypeError: invokeMember GraalVM native-image类型错误:invokeMember GraalVM native-image
【发布时间】:2020-05-23 10:32:13
【问题描述】:

我正在尝试使用 GraalVM 创建本机映像,我的代码:

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-image 时会引发错误。在错误后面跟着用于编译 native-image 的命令行。

错误

TypeError:JavaObject[com.compiler.commons.log.Console@113a2d320 (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

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

【问题讨论】:

    标签: java graalvm-native-image


    【解决方案1】:

    我已经解决了,我需要清理“/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
    

    【讨论】:

      猜你喜欢
      • 2020-07-22
      • 1970-01-01
      • 1970-01-01
      • 2021-12-05
      • 1970-01-01
      • 2020-12-07
      • 1970-01-01
      • 2016-10-24
      相关资源
      最近更新 更多