简体   繁体   English

类型错误:invokeMember GraalVM native-image

[英]TypeError: invokeMember GraalVM native-image

I'm trying create a native-image with GraalVM, my code:我正在尝试使用 GraalVM 创建本机图像,我的代码:

import org.graalvm.polyglot.HostAccess;

public class Console {

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

Then I call the code itself like this one:然后我像这样调用代码本身:

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());


    }

It's work fine when I run through IDE or java -jar, but When I try compile to native-image throws an error.当我通过 IDE 或 java -jar 运行时它工作正常,但是当我尝试编译到本机映像时会引发错误。 Bellow the error follow by the command-line used to compile native-image.在错误后面跟着用于编译本机映像的命令行。

Error :错误

TypeError: invokeMember (print) on JavaObject[com.compiler.commons.log.Console@113a2d320 (com.compiler.commons.log.Console)] failed due to: Unknown identifier: print类型错误:JavaObject [com.compiler.commons.log.Console@113a2d320 (com.compiler.commons.log.Console)] 上的调用成员(打印)失败,原因是:未知标识符:打印

Command-line命令行

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

Please could someone help me?请问有人可以帮我吗? thanks a lot多谢

I've solved, I needed clean from "/config" files, execute the application along agent and then compile adding some different parameters.我已经解决了,我需要清理“/config”文件,沿着代理执行应用程序,然后编译添加一些不同的参数。 See both below:见下文:

agent代理人

/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 

compile编译

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM