Jython - 集成Java和Python

时间:2016-07-08 08:54:17

标签: java python jython jython-2.7

我有示例python文件,我需要通过java程序调用。 这是使用Jython

Pom Dependency

<dependency>
    <groupId>org.python</groupId>
    <artifactId>jython-standalone</artifactId>
    <version>2.7.0</version>
</dependency>

Java文件

public class JythonIntegrationTest {

    public static void main(String[] args) throws FileNotFoundException , ScriptException {
        StringWriter writer = new StringWriter();

        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptContext context = new SimpleScriptContext();

        context.setWriter(writer);
        ScriptEngine engine = manager.getEngineByName("python");
        engine.eval(new FileReader("D:\\python\\sample.py") , context);
        System.out.println(writer.toString());
    }
}

当我运行此程序时,我得到以下错误: -

行是 - manager.getEngineByName("python");

Exception in thread "main" java.lang.NullPointerException
    at maven_test.maven_test.JythonIntegrationTest.main(JythonIntegrationTest.java:38)

我是否需要在系统上运行一些python exe / service。

0 个答案:

没有答案