通过脚本引擎(jython)从Java调用Python?
我正在尝试使用 javax.script 从 Java 6 应用程序调用 Jython:
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();
}
}
}
这会导致 NullPointerException:
java.lang.NullPointerException
at jythonEx.main(jythonEx.java:12)
有人知道我在这里做错了什么吗?
编辑:
感谢您的回复!我将 jython.jar 添加到类路径中,它运行正常:
java -cp "./;jython.jar" jythonEx
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须先注册您的引擎。
来自: ScriptEngineManager .getEngineByName:
用户指南显示 要将其与 JSR-223 一起使用,您必须:
您已经这样做了吗?编辑
关于您的评论:我认为您应该提出一个新问题,您会得到更好的答案。
You have to register your engine first.
From: ScriptEngineManager.getEngineByName:
The user guide says to use it with JSR-223 you have to:
Did you do that already?EDIT
About your comment: I think you should open a new question, you'll get better answers.
您可能必须为“python”注册一个 ScriptEngineFactory
You'd probably have to register a ScriptEngineFactory for'python'