我们可以从java调用python方法吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16460468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can we call a python method from java?
提问by vikkyhacks
I know jython allows us to call a java method from any java's classfile as if they were written for python, but is the reverse possible ???
我知道 jython 允许我们从任何 java 的类文件中调用 java 方法,就好像它们是为 python 编写的一样,但反过来可能吗???
I already have so many algorithms that written in python, they work pretty well with python and jython but they lack a proper GUI. I am planing to bring the GUI with the java and to keep the python library intact. I am not able to write a good GUI with jython or python and I cannot write a good algorithm with python. So the solution I found was to merge java's GUI and python's library. Is this possible. Can I call python's library from java.
我已经有很多用 python 编写的算法,它们与 python 和 jython 一起工作得很好,但它们缺乏适当的 GUI。我打算将 GUI 与 java 一起使用并保持 python 库完好无损。我无法使用 jython 或 python 编写好的 GUI,也无法使用 python 编写好的算法。所以我找到的解决方案是合并java的GUI和python的库。这可能吗。我可以从java调用python的库吗?
采纳答案by The Dark Knight
Yes, that can be done . Normally this will be done by creating a PythonInterpreter
object and then calling the python class using that .
是的,这是可以做到的。通常这将通过创建一个PythonInterpreter
对象然后使用该对象调用 python 类来完成。
Consider the following example :
考虑以下示例:
Java :
爪哇:
import org.python.core.PyInstance;
import org.python.util.PythonInterpreter;
public class InterpreterExample
{
PythonInterpreter interpreter = null;
public InterpreterExample()
{
PythonInterpreter.initialize(System.getProperties(),
System.getProperties(), new String[0]);
this.interpreter = new PythonInterpreter();
}
void execfile( final String fileName )
{
this.interpreter.execfile(fileName);
}
PyInstance createClass( final String className, final String opts )
{
return (PyInstance) this.interpreter.eval(className + "(" + opts + ")");
}
public static void main( String gargs[] )
{
InterpreterExample ie = new InterpreterExample();
ie.execfile("hello.py");
PyInstance hello = ie.createClass("Hello", "None");
hello.invoke("run");
}
}
Python :
Python :
class Hello:
__gui = None
def __init__(self, gui):
self.__gui = gui
def run(self):
print 'Hello world!'
回答by Dropout
You can easily call python functions from Java code with Jython. That is as long as your python code itself runs under jython, i.e. doesn't use some c-extensions that aren't supported.
If that works for you, it's certainly the simplest solution you can get. Otherwise you can use org.python.util.PythonInterpreter from the new Java6 interpreter support.
A simple example from the top of my head - but should work I hope: (no error checking done for brevity)
您可以使用 Jython 从 Java 代码轻松调用 python 函数。只要您的python 代码本身在jython 下运行,即不使用一些不受支持的c 扩展。
如果这对您有用,那肯定是您可以获得的最简单的解决方案。否则,您可以使用来自新 Java6 解释器支持的 org.python.util.PythonInterpreter。
我头顶上的一个简单示例 - 但我希望应该可以工作:(为简洁起见,没有进行错误检查)
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("import sys\nsys.path.append('pathToModiles if they're not there by default')\nimport yourModule");
// execute a function that takes a string and returns a string
PyObject someFunc = interpreter.get("funcName");
PyObject result = someFunc.__call__(new PyString("Test!"));
String realResult = (String) result.__tojava__(String.class);