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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

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

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

Jython是一個使用相當可靠的語法的快速Java腳本的好工具。 實際上,當使用jmx為您的Java應用程序實現一些維護或監視腳本時,它的運行效果非常好。

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

首先,讓我們使用獨立版本導入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' }

因此,最簡單的方法就是在我們的類路徑中執行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;/*** 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);}

下一步是創建一個python類文件和另一個將導入該類文件并實例化一個類的python文件。

該類文件將是divider.py。

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

導入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);}

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

使用解釋器運行文件是可以的,但是我們需要充分利用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;/*** 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

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。