I'm running Javascript code in my Java 15 application with GraalVM. It is a web application that runs on Tomee. It works fine, and now I also need to run Python code with GraalVM.
What are the libraries I need to include in pom.xml? I see several here:
https://mvnrepository.com/artifact/org.graalvm.python
This is what I have today in pom.xml:
<dependency>
<groupId>org.graalvm.js</groupId>
<artifactId>js</artifactId>
<version>22.1.0.1</version>
</dependency>
<dependency>
<groupId>org.graalvm.js</groupId>
<artifactId>js-scriptengine</artifactId>
<version>22.1.0.1</version>
</dependency>
should I change from graal.js to graal.sdk? I want to be able to run either Javascript or Python with the same engine.
Edit:
For JDK < 21 there aren't any Maven artifacts for GraalPy (Python on GraalVM). Moreover, previous releases of GraalPy for JDKs < 21 are not maintained. On top of that JDK15 is out of life, so "I hope this is not an important system." ;-)
However, you can use an older GraalVM release, ideally LST that is still supported, for example as of now 21.3. You can choose GraalVM 21.3 based on JDK17 or JDK11. There may be some other unsupported GraalVM releases based on JDK15 lying around too. Then download GraalPy for given GraalVM (https://github.com/oracle/graalpython/releases) and install it using $GRAALVM_HOME/bin/gu install -F the-graalpython-release.jar
. Then you use that GraalVM installation as you JDK (i.e., $JAVA_HOME). GraalPy will be "just" available out of the box there. No need to any dependencies. You can do the same for JavaScript.
If you can upgrade Java to 21 or later:
What you want for Python embedding in Java is:
org.graalvm.polyglot:python-community
(https://mvnrepository.com/artifact/org.graalvm.polyglot/python-community) ororg.graalvm.polyglot:python
(https://mvnrepository.com/artifact/org.graalvm.polyglot/python) for GraalPy Enterprise.These are mata-poms that include all the necessary dependencies from the org.graalvm.python
group and elsewhere. So your pom.xml
should look like:
<dependencies>
<dependency>
<groupId>org.graalvm.polyglot</groupId>
<artifactId>polyglot</artifactId>
<version>23.1.1</version>
</dependency>
<dependency>
<groupId>org.graalvm.polyglot</groupId>
<artifactId>python-community</artifactId>
<version>23.1.1</version>
<type>pom</type>
</dependency>
</dependencies>
and then you can do things like:
try (var ctx = org.graalvm.polyglot.Context.create("python")) {
ctx.eval("python", "print('Hello world')");
}
It seems that you are using JavaScript via the Script engine API. This is not supported for Python. One can find some example Script engine implementations, but the recommended approach is to use the Context
API. It is language agnostic, so you can use it for both Python and JavaScript.