有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

python使用javax从Java调用Jython。脚本

我正在尝试使用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引擎。罐子在外面,但如果我能找到它,我就被绞死了


共 (1) 个答案

  1. # 1 楼答案

    Perthis question,使用jython-standalone.jar而不是jython.jar返回非空引擎:

    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>

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