Search code examples
javascriptjavanashorngraalvm

Use java class in Graal.js


Using Graal.js, how can I import a java class to script within JS?

The following code works with Nashorn JJS, but does not work with Graal.js because there is no Java.type() in graal, do I need to invoke truffle at some point?

var ArrayList = Java.type("java.util.ArrayList");
var myList = new ArrayList();
myList.add("hello");
myList.add("world");
print(myList);

EDIT: I was able to get it to import java types using the --jvm paramter, which seems to indicate that it runs it on the JVM. So does this completely pas over the GraalVM and just use the JVM?


Solution

  • add the --jvm arg to the JS command to run Graal.js on JVM, this will allow access to Java objects.