Make Jython call from Java using javax.scripting

房东的猫 提交于 2019-12-11 03:49:27

问题


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

The code I added is:

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

Why am I getting a null python engine?

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.


回答1:


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

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

Curious, but at least I can move on.



来源:https://stackoverflow.com/questions/32998167/make-jython-call-from-java-using-javax-scripting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!