如何从嵌入式 Jython 中调用 java 函数?
我创建了一个 PythonInterpreter 对象,并且想要调用 java 函数,但不断收到错误:
Exception in thread "main" Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'jytest2' is not defined
Java Result: 1
How do you call a java function from a live running system?
public static void main(String args[])
{
ModRet modRet = new ModRet();
jytest();
}
public void jytest()
{
PythonInterpreter interp = new PythonInterpreter();
interp.exec("print \'Hello; jython has successfully been embedded!\'");
interp.exec("print " + FPS);
interp.exec("jytest2()");
}
public void jytest2()
{
System.out.println("HIHIHI");
}
I have created an PythonInterpreter object, and want to call a java function but keep getting the error:
Exception in thread "main" Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'jytest2' is not defined
Java Result: 1
How do you call a java function from a live running system?
public static void main(String args[])
{
ModRet modRet = new ModRet();
jytest();
}
public void jytest()
{
PythonInterpreter interp = new PythonInterpreter();
interp.exec("print \'Hello; jython has successfully been embedded!\'");
interp.exec("print " + FPS);
interp.exec("jytest2()");
}
public void jytest2()
{
System.out.println("HIHIHI");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通常发现我需要使用对象工厂模式并通过接口调用事物,如 Jython Book - 第 10 章
我仍然不太清楚您想要完成的任务,所以我没有任何代码适合您;但我相信你会发现这本书很有帮助。
I generally find that I need to use the Object Factory pattern and invoke things via an Interface as described in the Jython Book - Chapter 10
I'm still not super-clear on what you're trying to accomplish, so I don't have any code for you; but I'm sure you'll find the book helpful.
默认情况下,当前类路径包含在 jython 路径中。要访问路径中的任何方法,您应该执行导入。
如果您想访问 jython 中的 java 实例,可以使用 py.set(string, object) 使其在上下文中可访问。 (就像 javax.scripting 一样)
By default, the current classpath is included in jython path. To access any method in the path, you should do an import.
And if you want to access a java instance in jython, you can use
py.set(string, object)
to make it accessible in the context. (Just like javax.scripting)