I created a default Maven Java project and added the following dependency:
<dependency>
<groupId>org.python</groupId>
<artifactId>jython</artifactId>
<version>2.7.0</version>
</dependency>
Then I created a package src/main/java/mypkg
and added this class:
package mypkg;
import javax.script.*;
class JythonMinimalTest {
public static void main(String[] args) throws Exception {
String engineName = "python";
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName(engineName);
if (engine == null) {
System.err.println("ERROR: `" + engineName + "` not available.");
System.err.println("Available engines: ");
for (ScriptEngineFactory factory: manager.getEngineFactories()) {
System.err.println(factory);
System.err.println("names:");
for (String name: factory.getNames()) {
System.err.println(" " + name);
}
}
System.exit(999);
}
engine.eval("print('hello, world')");
}
}
When I run it using exec:java
,
mvn exec:java -Dexec.mainClass=mypkg.JythonMinimalTest
I get the following mysterious output:
ERROR: `python` not available.
Available engines:
org.python.jsr223.PyScriptEngineFactory@2b0e6c89
names:
python
jython
jdk.nashorn.api.scripting.NashornScriptEngineFactory@46618cb8
names:
nashorn
Nashorn
js
JS
JavaScript
javascript
ECMAScript
ecmascript
The manager returns null
, but then in the next line lists python
/jython
among the available script engines.
Nashorn worked just fine in exactly the same way. What am I doing wrong with Jython?