1

I have a little maven project to learn about scripting in Java using javax.script API, namely JavaScript and Python. While JavaScript works well, like e.g. in this method

@Test
public void TestPrintHelloWorldJs() throws ScriptException {
    ScriptEngineManager factory = new ScriptEngineManager();
    ScriptEngine engine = factory.getEngineByName("nashorn");
    
    engine.eval("print('Hello, World')"); // works
}

I cannot get python running at all because the returned engine is always null:

@Test
public void TestPrintHelloWorldPy() throws ScriptException {
    ScriptEngineManager factory = new ScriptEngineManager();
    ScriptEngine engine = factory.getEngineByName("python"); // returns null !!!
    engine = factory.getEngineByName("jython"); // still returns null !!!

    engine.eval("print('Hello, World')"); // NullPointerException here
}

Any ideas what I can do to make it work?


Here are the relevant parts of my pom:

<properties>
    <maven.compiler.release>15</maven.compiler.release> <!-- Java 15 -->
</properties>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId> <!-- for testing -->
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.openjdk.nashorn/nashorn-core -->
    <dependency>
        <groupId>org.openjdk.nashorn</groupId>
        <artifactId>nashorn-core</artifactId> <!-- for javascript-->
        <version>15.3</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.python/jython -->
    <dependency>
        <groupId>org.python</groupId>
        <artifactId>jython</artifactId> <!-- for python -->
        <version>2.7.2</version>
    </dependency>
</dependencies>

I use the method from https://stackoverflow.com/a/38039047/5333340 in order to print out available engines, and the output I get is:

ScriptEngineFactory Info
    Script Engine: OpenJDK Nashorn (15.3)
    Engine Alias: nashorn
    Engine Alias: Nashorn
    Engine Alias: js
    Engine Alias: JS
    Engine Alias: JavaScript
    Engine Alias: javascript
    Engine Alias: ECMAScript
    Engine Alias: ecmascript
    Language: ECMAScript (ECMA - 262 Edition 5.1)
ScriptEngineFactory Info
    Script Engine: jython (2.7.2)
    Engine Alias: python
    Engine Alias: jython
    Language: python (2.7)

So the engine IS found. But why can't I get it? I double-checked that the arguments in getEngineByName have no typos.

I even installed python 2.7 on my computer (although I don't think this is actually necessary; the implementation should be inside the jython package), but it did not change anything.

1 Answer 1

2

Seems I wasn't using the right dependency...

I replaced

<!-- https://mvnrepository.com/artifact/org.python/jython -->
<dependency>
    <groupId>org.python</groupId>
    <artifactId>jython</artifactId>
    <version>2.7.2</version>
</dependency>

by

<!-- https://mvnrepository.com/artifact/org.python/jython-standalone -->
<dependency>
    <groupId>org.python</groupId>
    <artifactId>jython-standalone</artifactId>
    <version>2.7.2</version>
</dependency>

and that's it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.