简体   繁体   中英

TypeError: invokeMember GraalVM native-image

I'm trying create a native-image with GraalVM, my code:

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. 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

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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