日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

将Jython嵌入到您的Java代码库中

發(fā)布時間:2023/12/3 java 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 将Jython嵌入到您的Java代码库中 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Jython是一個使用相當(dāng)可靠的語法的快速Java腳本的好工具。 實際上,當(dāng)使用jmx為您的Java應(yīng)用程序?qū)崿F(xiàn)一些維護(hù)或監(jiān)視腳本時,它的運(yùn)行效果非常好。

如果您與其他具有python背景的團(tuán)隊合作,則將python集成到您的java應(yīng)用程序是絕對有意義的。

首先,讓我們使用獨(dú)立版本導(dǎo)入jython interpeter。

group 'com.gkatzioura' version '1.0-SNAPSHOT'apply plugin: 'java'sourceCompatibility = 1.5repositories {mavenCentral() }dependencies {testCompile group: 'junit', name: 'junit', version: '4.11'compile group: 'org.python', name: 'jython-standalone', version: '2.7.0' }

因此,最簡單的方法就是在我們的類路徑中執(zhí)行python文件。 該文件將是hello_world.py

print "Hello World"

然后將文件作為輸入流傳遞給干預(yù)者

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;/*** Created by gkatzioura on 19/10/2016.*/ public class JythonCaller {private PythonInterpreter pythonInterpreter;public JythonCaller() {pythonInterpreter = new PythonInterpreter();}public void invokeScript(InputStream inputStream) {pythonInterpreter.execfile(inputStream);}}@Testpublic void testInvokeScript() {InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("hello_world.py");jythonCaller.invokeScript(inputStream);}

下一步是創(chuàng)建一個python類文件和另一個將導(dǎo)入該類文件并實例化一個類的python文件。

該類文件將是divider.py。

class Divider:def divide(self,numerator,denominator):return numerator/denominator;

導(dǎo)入Divider類的文件將是classcaller.py

from divider import Dividerdivider = Divider()print divider.divide(10,5);

所以讓我們測試一下

@Testpublic void testInvokeClassCaller() {InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("classcaller.py");jythonCaller.invokeScript(inputStream);}

從這個例子中我們可以理解的是,解釋器成功地從類路徑中導(dǎo)入了文件。

使用解釋器運(yùn)行文件是可以的,但是我們需要充分利用python中實現(xiàn)的類和函數(shù)。
因此,下一步是創(chuàng)建一個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;/*** Created by gkatzioura on 19/10/2016.*/ 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上找到源代碼。

翻譯自: https://www.javacodegeeks.com/2016/10/embed-jython-java-codebase.html

總結(jié)

以上是生活随笔為你收集整理的将Jython嵌入到您的Java代码库中的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。