2015-10-07 38 views
1

我正在嘗試使用Jython將一個小Python腳本集成到我擁有的Java程序中。我似乎無法使用javax.script軟件包獲取python/jython引擎。使用javax.scripting從Java進行Jython調用

我從here代碼加入少量生產這樣的:

[email protected]:~$ echo $CLASSPATH 
/opt/jython/jython.jar:. 
[email protected]:~$ 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 
[email protected]:~$ 

我添加的代碼是:

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

爲什麼我收到一個空蟒蛇引擎?

我跑過this bug,這似乎表明有(或有)jython-engine.jar在那裏,但如果我能找到它,我會被絞死。

+0

嘗試'「py」'而不是'「python」'。 –

+0

@ElliottFrisch同樣的結果,如果我使用「py」作爲名稱。我也試過'manager.getEngineByExtension(「py」)'無濟於事。 – Andrew

+0

您使用的是什麼版本的'java'? –

回答

2

this question,使用jython-standalone.jar而不是jython.jar返回一個非空的引擎:

[email protected]:~$ export CLASSPATH=jython-standalone-2.7.0.jar:. 
[email protected]:~$ 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 
[email protected]:~$ 

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

好奇,但至少我可以繼續前進。謝謝閱讀!