TypeError:invokeMember GraalVM本机图像

时间:2020-05-23 10:32:13

标签: java graalvm-native-image

我正在尝试使用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运行时,它工作正常,但是当我尝试编译为本机映像时会抛出错误。出现错误后,将使用命令行来编译本地图像。

错误

TypeError:JavaObject [com.compiler.commons.log.Console@113a2d320(com.compiler.commons.log.Console)]上的invokeMember(打印)]由于以下原因而失败:未知标识符:print

命令行

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

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

1 个答案:

答案 0 :(得分:0)

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