Last active
July 31, 2021 14:00
-
-
Save PhilippSalvisberg/282d0b88f2cd907f90d28ff5e25cb5a0 to your computer and use it in GitHub Desktop.
GraalVM native image example to reproduce Exception in thread "main" javax.script.ScriptException: org.graalvm.polyglot.PolyglotException: TypeError: invokeMember (collect) on java.util.stream.ReferencePipeline$Head@4c320659 failed due to: Unknown identifier: collect
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
javac Demo.java | |
$JAVA_HOME/bin/native-image -H:ReflectionConfigurationFiles=reflection-config.json --language:js --no-fallback Demo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.oracle.truffle.js.scriptengine.GraalJSScriptEngine; | |
import org.graalvm.polyglot.Context; | |
import javax.script.ScriptContext; | |
import javax.script.ScriptEngine; | |
import javax.script.ScriptException; | |
import java.util.HashMap; | |
public class Demo { | |
public static void main(String[] args) throws ScriptException { | |
var map = new HashMap<>(); | |
map.put("1", "one"); | |
map.put("2", "two"); | |
ScriptEngine scriptEngine = GraalJSScriptEngine.create(null, | |
Context.newBuilder("js").allowAllAccess(true)); | |
scriptEngine.getContext().setAttribute("map", map, ScriptContext.ENGINE_SCOPE); | |
String script = "var Collectors = Java.type('java.util.stream.Collectors');\n" + | |
"var keys = map.keySet().stream().collect(Collectors.toList());\n" + | |
"for (var i in keys) {\n" + | |
" print ('key: ' + keys[i] + ' value: ' + map.get(keys[i]));\n" + | |
"}"; | |
scriptEngine.eval(script); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"name" : "java.util.HashMap", | |
"allDeclaredConstructors": true, | |
"allDeclaredMethods": true, | |
"allDeclaredFields": true, | |
"allDeclaredClasses": true, | |
"allPublicConstructors": true, | |
"allPublicMethods": true, | |
"allPublicFields": true, | |
"allPublicClasses": true | |
}, | |
{ | |
"name" : "java.util.HashMap$KeySet", | |
"allDeclaredConstructors": true, | |
"allDeclaredMethods": true, | |
"allDeclaredFields": true, | |
"allDeclaredClasses": true, | |
"allPublicConstructors": true, | |
"allPublicMethods": true, | |
"allPublicFields": true, | |
"allPublicClasses": true | |
}, | |
{ | |
"name" : "java.util.stream.ReferencePipeline", | |
"allDeclaredConstructors": true, | |
"allDeclaredMethods": true, | |
"allDeclaredFields": true, | |
"allDeclaredClasses": true, | |
"allPublicConstructors": true, | |
"allPublicMethods": true, | |
"allPublicFields": true, | |
"allPublicClasses": true | |
}, | |
{ | |
"name" : "java.util.stream.ReferencePipeline$Head", | |
"allDeclaredConstructors": true, | |
"allDeclaredMethods": true, | |
"allDeclaredFields": true, | |
"allDeclaredClasses": true, | |
"allPublicConstructors": true, | |
"allPublicMethods": true, | |
"allPublicFields": true, | |
"allPublicClasses": true | |
}, | |
{ | |
"name" : "java.util.stream.Collectors", | |
"allDeclaredConstructors": true, | |
"allDeclaredMethods": true, | |
"allDeclaredFields": true, | |
"allDeclaredClasses": true, | |
"allPublicConstructors": true, | |
"allPublicMethods": true, | |
"allPublicFields": true, | |
"allPublicClasses": true | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment