1

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.

asked Dec 24, 2023 at 3:01

1 Answer 1

1
+200

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:

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.

answered Dec 27, 2023 at 12:06
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, Polyglot is supported in Java 15?
In Java 21. In general in LTS version + the latest, so we will have 22 soon and 21 will still be supported.
I need to run python code in Java 15, as the title says
Thanks Steve, this is my issue, we developed the java application two years ago with JDK 15, and in two years it became obsolete. If we change now to JDK 21, it will also become obsolete in two additional years. We cannot upgrade JDK only to implement python and cannot implement either a non-supported version of GraalVM, so we will find another solution that is not GraalVM based.
OpenJDK 15 is already end of life. OpenJDK 17 is eof at 2027 and OpenJDK 21 is eof at 2029.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.