上海:15201841284
广州:020-2989 6995
深圳:0755-23061965
武汉:027-8798 9193
西安:029-8822 8155
Jython 是一个很棒的 Java 脚本引擎,使用很可靠的语法。事实上它非常使用用来编写一些维护或者监控脚本。
如果你和其他具备 Python 经验的团队协作,那么 Jython 可以很方便的在你的 Java 应用中集成 Python 代码。
更多精彩内容以及学习资料,尚学堂论坛bbs.bjsxt.com免费下载。
首先,我们需要导入 Jython 解释器:
group 'com.gkatzioura' version '1.0-SNAPSHOT' apply plugin: 'java' sourceCompatibility = 1.5 repositories { mavenCentral() } dependencies { testCompile group: 'junit', name: 'junit', version: '4.11' compile group: 'org.python', name: 'jython-standalone', version: '2.7.0' }
最简单的是直接执行类路径中的某个 Python 文件,例如有这么一个 Python 文件:hello_world.py
print "Hello World"
然后将这个文件作为解析流传递给解析器:
package com.gkatzioura; import org.python.core.PyClass; import org.python.core.PyInteger; import org.python.core.PyObject; import org.python.core.PyObjectDerived; import org.python.util.PythonInterpreter; import java.io.InputStream; public class JythonCaller { private PythonInterpreter pythonInterpreter; public JythonCaller() { pythonInterpreter = new PythonInterpreter(); } public void invokeScript(InputStream inputStream) { pythonInterpreter.execfile(inputStream); } }
@Test public void testInvokeScript() { InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("hello_world.py"); jythonCaller.invokeScript(inputStream); }
下一步是创建一个 Python 类文件和另外一个 Python 文件导入类文件并对类进行实例化。
这个类文件假设为 divider.py.
class Divider: def divide(self,numerator,denominator): return numerator/denominator;
而导入 Divider 类的文件名为 classcaller.py
from divider import Divider divider = Divider() print divider.divide(10,5);
接下来进行测试:
@Test public void testInvokeClassCaller() { InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("classcaller.py"); jythonCaller.invokeScript(inputStream); }
从这里例子中我们可以看到解析器已经成功的导入了 Python 源文件和响应的库包。
使用解析器运行 Python 源码文件是没问题的。不过我们还需要完全利用 Python 中实现的类和方法。
因此,接下来是创建一个 Python 类,并在 Java 中使用其定义的函数。
package com.gkatzioura; import org.python.core.PyClass; import org.python.core.PyInteger; import org.python.core.PyObject; import org.python.core.PyObjectDerived; import org.python.util.PythonInterpreter; import java.io.InputStream; public class JythonCaller { private PythonInterpreter pythonInterpreter; public JythonCaller() { pythonInterpreter = new PythonInterpreter(); } public void invokeClass() { pythonInterpreter.exec("from divider import Divider"); PyClass dividerDef = (PyClass) pythonInterpreter.get("Divider"); PyObject divider = dividerDef.__call__(); PyObject pyObject = divider.invoke("divide",new PyInteger(20),new PyInteger(4)); System.out.println(pyObject.toString()); } }
完整源码请访问 GitHub.
更多精彩内容以及学习资料,尚学堂论坛bbs.bjsxt.com免费下载。
北京京南校区:北京亦庄经济开发区科创十四街6号院1号楼 赛蒂国际工业园
咨询电话:400-009-1906 / 010-56233821
面授课程: JavaEE+微服务+大数据 大数据+机器学习+平台架构 Python+数据分析+机器学习 人工智能+模式识别+强化学习 WEB前端+移动端+服务端渲染
地址:陕西省西安市高新区西安软件园西区创新信息大厦A座三层尚学堂
电话:029-88228155 / 18291433445
山西学区地址:山西省晋中市榆次区大学城大学生活广场万科商业A1座702
武汉学区地址:武汉市东湖高新区光谷金融港B22栋11楼
咨询电话:027-87989193
网址:http://www.cssxt.com/
咨询电话:0731-83072091
深圳校区地址:深圳市宝安区航城大道U8智造产业园U6栋3楼
咨询电话:0755-23061965 / 18898413781
上海尚学堂校区地址:上海市浦东新区城丰路650号
咨询电话:021-67690939
广州校区地址:广州市天河区车陂街道大岗路5号中侨广场2栋321室(四号线车陂站D出口,或brt车陂站)
咨询电话:18948349646
保定招生办公室
地址:河北省保定市竞秀区朝阳南大街777号鸿悦国际1101室
电话:15132423123