Graal: [Question] Is it possible to load a .jar-file from guest-languages?

Created on 18 Feb 2019  路  4Comments  路  Source: oracle/graal

The Reference Manual shows how to use java API classes from guest languages.

Now, let's assume that we have written some User-class in Java and that we have compiled this class (and its dependencies) in a jar library. Let's further assume that we want to use this User-class in our guest language (e.g. JavaScript or Python). Is it possible to load the jar archive from a guest language and use it from a guest language? If so: could you extend the aforementioned manual by samples for this case?

Just to be clear: I know that one could start off the process through a java-process by wrapping the initial call to the guest-language in a Java-class. This is not what I am looking for. I am looking for a way to load java libraries when starting some program through js, graalpython,...

question

All 4 comments

Hi,

I don't know of any limitation in that area. If you start from js --jvm, you can interop to Java and do anything you can normally do in Java, including accessing JAR files as you are used to.

You can even load the JAR directly from JavaScript:

var File = Java.type("java.io.File");
var URL = Java.type("java.net.URL");
var URLClassLoader = Java.type("java.net.URLClassLoader");

var file  = new File("mydemo.jar");
var urls = [file.toURL()];

var cl = new URLClassLoader(urls);
var cls = cl.loadClass("mypackage.MyClass");

print(cls);

We could add a simple example with best practices to the website, true.

edit: you can of course do js --jvm --jvm.cp=mydemo.jar:. myscript.js right away, to only do Java.type('mypackage.MyClass'); in JavaScript.

Best,
Christian

Note you can also add elements to the classpath dynamically from JavaScript, via Java.addToClasspath(filename).

This was pretty much what I was looking for :) should definitively be mentioned on the website.

Hi,

I've added very basic documentation to the doc files in the graaljs repo (https://github.com/graalvm/graaljs/commit/588cfedcc4abf9d104a8e406c935e116b297fd72), but we will still need to add this to the website. Thanks for the hint.

-- Christian

Was this page helpful?
0 / 5 - 0 ratings