使用 javax.scripting 从 Java 进行 Jython 调用

Make Jython call from Java using javax.scripting

我正在尝试使用 Jython 将一个小 Python 脚本集成到我的 Java 程序中。我似乎无法使用 javax.script 包获得 python/jython 引擎。

我从 here 中提取了代码,并添加了少量内容来生成此代码:

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:~$

我添加的代码是:

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));
}

为什么我得到一个空 python 引擎?

我 运行 横跨 this bug,这似乎表明那里有(或曾经有)一个 jython-engine.jar,但如果我能找到它,我会被绞死.

根据 this question,使用 jython-standalone.jar 而不是 jython.jar returns 非空引擎:

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:~$

(在我的pom.xml中,我使用了<artifactId>jython-standalone</artifactId>

很好奇,但至少我可以继续前进。