3

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?

1
  • When you use --jvm you are still running with Graal.js. It's a mode where you run Graal.js inside the JVM which gives you access to all the JVM features. The default mode (--native) runs in a pre-compiled native binary that is faster to start-up but does not support loading Java classes. Commented Dec 10, 2017 at 10:16

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.