Search code examples
rgraalvm

Use R / Pyhon from GraalVM on Non-Graal JDK


Is it possible to use R and Python using GraalVM / Truffle in a Java program on a standard OpenJDK / OracleJDK? I am able to run Javascript by simply including org.graalvm.js and org.graalvm.truffle in Maven, but I cannot find packages for R or Python to do the same.

Ideally the application would always run in GrallVM and it wouldn't be an issue, however I do not have any control over which actual VM my customers use for my application. Although I may recommend GraalVM, my customers might have valid reasons for using alternate JVMs since they will generally have to perform their own internal security audits and validations and would therefore accept only the specific VMs that they've certified for use in their environments which may or may not include GraalVM.


Solution

  • Starting from version 23.1, all GraalVM additional languages can now be easily embedded in Java, including non-Graal JDKs (but with caveats!) by using Maven artifacts published on Mavencentral. Those artifacts embed and on-demand extract the necessary files.

    So using GraalPy in Java is as simple as adding this to pom.xml (or equivalent code for the build system of your choice):

        <dependencies>
            <dependency>
                <groupId>org.graalvm.polyglot</groupId>
                <artifactId>polyglot</artifactId>
                <version>23.1.0</version>
            </dependency>
            <dependency>
                <groupId>org.graalvm.polyglot</groupId>
                <artifactId>python-community</artifactId>
                <version>23.1.0</version>
                <type>pom</type>
            </dependency>
        </dependencies>
    

    then one can run Python code via GraalPy just like so:

    try (var ctx = org.graalvm.polyglot.Context.create()) {
        ctx.eval("python", "print('Hello world')"); 
    }
    

    The caveat for non-GraalVM JDKs is that they do not support JIT compilation of Python code. You will get a warning like this:

    The polyglot engine uses a fallback runtime that does not support runtime compilation to native code.
    Execution without runtime compilation will negatively impact the guest application performance.
    The following cause was found: JVMCI is not enabled for this JVM. Enable JVMCI using -XX:+EnableJVMCI.
    For more information see: https://www.graalvm.org/latest/reference-manual/embed-languages/.
    To disable this warning use the '--engine.WarnInterpreterOnly=false' option or the '-Dpolyglot.engine.WarnInterpreterOnly=false' system property.
    

    Old answer applicable to GraalVM/GraalPy version < 21.3.0

    In short: GraalVM R and GraalVM Python, unlike GraalVM JavaScript, need more than just a jar file to run: the standard library and in case of R also native libraries. Python can do without native libraries, but using them improves compatibility.

    You could bundle all that and configure the right options for your stock JDK, but for the time being this is not a scenario that is actively supported by those projects. Not saying it wouldn't work.

    Details: https://github.com/oracle/graalpython/issues/96