java python混合编程_通过脚本引擎(jython)从Java调用Python?

I'm trying to call Jython from a Java 6 application using javax.script:

import javax.script.ScriptEngine;

import javax.script.ScriptEngineManager;

import javax.script.ScriptException;

public class jythonEx

{

public static void main (String args[]) throws ScriptException

{

ScriptEngineManager mgr = new ScriptEngineManager();

ScriptEngine pyEngine = mgr.getEngineByName("python");

try {

pyEngine.eval("print \"Python - Hello, world!\"");

} catch (Exception ex) {

ex.printStackTrace();

}

}

}

This is causing a NullPointerException:

java.lang.NullPointerException

at jythonEx.main(jythonEx.java:12)

Does anyone have any idea what I'm doing wrong here?

Edit:

Thanks for the responses! I added jython.jar to the classpath and it runs properly:

java -cp "./;jython.jar" jythonEx

解决方案

You have to register your engine first.

[...] first searches for a ScriptEngineFactory that has been registered as a handle [...] Returns null if no such factory was found

The user guide says to use it with JSR-223 you have to:

As of Jython 2.5.1 an implementation of JSR 223 is bundled in jython.jar. Simply add jython to your CLASSPATH and ask for the python script engine.

Did you do that already?

EDIT

About your comment: I think you should open a new question, you'll get better answers.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值