本文介绍了使用javax.scripting从Java进行Jython调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我添加的代码是:

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引擎?

Why am I getting a null python engine?

我遇到了此错误,这似乎表明存在(或曾经是)jython- engine.jar在那里,但是如果我找到它,我就被绞死了.

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.

推荐答案

每,使用jython-standalone.jar而不是jython.jar返回一个非空引擎:

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

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

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

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

Curious, but at least I can move on.

这篇关于使用javax.scripting从Java进行Jython调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 01:48