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