If you want to execute some piece of python script code in your java programm, you may have two good options to achieve this.
One is "javax.script" , the other is "jython"
Both of them could open a python intepreter in your programm, and execute python script.
1. javax.script
2 Jython
jython is java implementation of python. In this approach, you need to install jython, then from the build package, find out jython.jar.
Import this jar to your project. We need to use classes e.g "org.python.util.PythonInterprete" in this jar.
See also : http://jythonpodcast.hostjava.net/jythonbook/chapter10.html
3. If you just want to execute a python script file. You can use Runtime.exec() to fork a process and execute the command.
The key point of these approach is "Set correct environment variables to JVM"
Below is a example.
What's more in this example? Standard output stream - stdout in python script will be redirect to java inputstream, this is extremly
useful when you want to retrieve data / check the result from the python script.
本文介绍了两种在Java中执行Python脚本的方法:使用javax.script和Jython。通过示例代码展示了如何设置环境变量并捕获Python脚本的标准输出。
4万+

被折叠的 条评论
为什么被折叠?



