【问题标题】:Make Jython call from Java using javax.scripting使用 javax.scripting 从 Java 进行 Jython 调用
【发布时间】:2015-10-07 16:57:35
【问题描述】:

我正在尝试使用 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,但如果我能找到它,我就会被绞死。

【问题讨论】:

  • 尝试"py" 而不是"python"
  • @ElliottFrisch 如果我使用“py”作为名称,则结果相同。我也试过manager.getEngineByExtension("py") 无济于事。
  • 你使用的是什么版本的java

标签: java python jython-2.7


【解决方案1】:

根据this 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>

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-06
    • 2015-09-05
    • 1970-01-01
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多