gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:41:04 25 4
gpt4 key购买 nike

我正在尝试使用 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返回一个非空引擎:

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

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

关于java - 使用 javax.scripting 从 Java 进行 Jython 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32998167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com