javapythonjython-2.7

Make Jython call from Java using javax.scripting


I'm trying to use Jython to integrate a small Python script into a Java program I have. I can't seem to get the python/jython engine using the javax.script package.

I took the code from here with a small addition to produce this:

andrew@asimov:~$ echo $CLASSPATH
/opt/jython/jython.jar:.
andrew@asimov:~$ java Engines
The following 2 scripting engines were found

Engine name: jython
    Version: 2.7.0
    Language: python
    Engine supports the following extensions:
            py
    Engine has the following short names:
            python
            jython
=========================
Engine name: Rhino
    Version: Rhino 1.7 release 4 2013 08 27
    Language: ECMAScript
    Engine supports the following extensions:
            js
    Engine has the following short names:
            js
            rhino
            JavaScript
            javascript
            ECMAScript
            ecmascript
=========================
python engine is null: true
js engine is null: false
andrew@asimov:~$

The code I added is:

String[] engineNames = new String[] {
        "python", "js"
};

for (String engineName : engineNames) {
    ScriptEngine engine = manager.getEngineByName(engineName);
    System.out.printf("%s engine is null: %s\n", engineName, (engine == null));
}

Why am I getting a null python engine?

I ran across this bug, which seemed to suggest that there is (or was) a jython-engine.jar out there, but I'm hanged if I could find it.


Solution

  • Per this question, using jython-standalone.jar rather than jython.jar returns a non-null engine:

    andrew@asimov:~$ export CLASSPATH=jython-standalone-2.7.0.jar:.
    andrew@asimov:~$ java Engines | tail -11
    Engine name: jython
        Version: 2.7.0
        Language: python
        Engine supports the following extensions:
                py
        Engine has the following short names:
                python
                jython
    =========================
    python engine is null: false
    javascript engine is null: false
    andrew@asimov:~$
    

    (In my pom.xml, I used <artifactId>jython-standalone</artifactId>)

    Curious, but at least I can move on.