I will add helpful answers from graalpy devs here too.
the answer related to my question are
Holger Hees Dienstag um 13:45 Uhr
Hello. I try to get graalpy running in my application. Currently I’m using jdk21 and this is my pom configuration
This is the problematic code
import org.graalvm.python.embedding.utils.GraalPyResources; public final class GraalPythonScriptEngineFactory { private final Engine polyglotEngine = GraalPyResources.contextBuilder(PYTHON_DEFAULT_PATH).allowAllAccess(true) .allowHostAccess(HostAccess.ALL).build().getEngine();
the maven build creates a jar file which is used in another applicationI already checked that the jar file includes org.graalvm.* [collections,home,nativeimage,options,polyglot,python,word] and org.graalvm.python.vfs.* etcif I start the other application which includes my jar I get the following error
Caused by: java.lang.ExceptionInInitializerError: Exception java.lang.IllegalStateException: No language and polyglot implementation was found on the module-path. Make sure at last one language is added to the module-path. [in thread "main"] at org.graalvm.polyglot.Engine$PolyglotInvalid.noPolyglotImplementationFound(Engine.java:1801) ~[?:?] at org.graalvm.polyglot.Engine$PolyglotInvalid.createHostAccess(Engine.java:1792) ~[?:?] at org.graalvm.polyglot.Engine$PolyglotInvalid.createHostAccess(Engine.java:1754) ~[?:?] at org.graalvm.polyglot.Engine$Builder.build(Engine.java:741) ~[?:?] at org.graalvm.polyglot.Context$Builder.build(Context.java:1925) ~[?:?] at org.openhab.automation.pythonscripting.internal.graal.GraalPythonScriptEngineFactory.<init>(GraalPythonScriptEngineFactory.java:69) ~[?:?] at org.openhab.automation.pythonscripting.internal.PythonScriptEngineFactory.<clinit>(PythonScriptEngineFactory.java:57) ~[?:?]
my understanding was that the language and polyglot implementation is embedded into my jar file with the maven plugin "graalpy-maven-plugin"thanks for your help (bearbeitet)
8 Antworten
Tim Felgentreff Dienstag um 14:04 Uhr
Is this a Graal JDK? Then you’ll need to use the latest JDK 23. If this is a fat jar, you’ll need some extra config, @Christian Humer can point you to it
Holger Hee Dienstag um 14:09 Uhr
no, it is the normal oracle jdk. I understood that all dependencies come in via the maven dependencies.
The fat jar already includes lib-graalpython stuff like libposix.so, libpython-native.so etc… and lib-python with a lot of python files. My understanding was that this is the embedded python runtime.
Christian Humer Dienstag um 15:20 Uhr
I’d recommend against using single fat jars. But if need to we have config for the shade plugin here:
polyglot-embedding-demo/pom.xml at main · graalvm/polyglot-embedding-demo · GitHub for the Maven Assembly plugin here:
polyglot-embedding-demo/pom.xml at main · graalvm/polyglot-embedding-demo · GitHub
Holger Hees Dienstag um 15:58 Uhr
It must be a single fat jar, because it it some kind of plugin for a smarthome system (openhab.org)The goal is to just add/install the jar to enable python support as a scripting languageThere are already jars/plugins for javascript, groovy and ruby and a obsolete jar for jython supportMy goal is to replace the jython support with graalpythonAnd thanks for the hint, I will look into theese maven plugins.
Tim Felgentreff Dienstag um 17:02 Uhr
Cool about openhab, would be awesome to get python 3 support with graalpy in there
Christian Humer Dienstag um 17:52 Uhr
And thanks for the hint, I will look into these maven plugins
You are welcome. These are the configurations we test with. If you run into some trouble with other tools, let us know. Unfortunately lots of tooling in that space just discards essential files from our jars by default. (hence my recommendation against using it) (bearbeitet)
Stepan Sindelar Dienstag um 20:21 Uhr
Once you get past that issue, then:
private final Engine polyglotEngine = GraalPyResources.contextBuilder(PYTHON_DEFAULT_PATH).allowAllAccess(true) .allowHostAccess(HostAccess.ALL).build().getEngine();
also looks suspicious.
- Normally you’d build
Engine
using Engine.newBuilder()...build()
and then you pass that Engine
to the contexts you create: GraalPyResources.contextBuilder(...).engine(polyglotEngine)...build()
.
- What is
PYTHON_DEFAULT_PATH
? If it is a path on a filesystem, then you first need to extract the GraalPy resources to that directory using GraalPyResources#extractVirtualFileSystemResources
, if you want GraalPy to use the resources directly from the jar where possible, then you’d use parameter-less GraalPyResources#contextBuilder
or overload taking VirtualFileSystem
, which you can create using VirtualFileSystem.newBuidler()...