0

我正在尝试使用 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) 失败,原因是:未知标识符: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

请问有人可以帮我吗?多谢

4

1 回答 1

0

I've solved, I needed clean from "/config" files, execute the application along agent and then compile adding some different parameters. 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
于 2020-05-24T09:39:44.950 回答