Java調(diào)用python的方法(jython)
1 什么是jython?
他其實(shí)是一門語言,并非是Java 或者Python的解釋器.用它可以實(shí)現(xiàn),java和python代碼的互相訪問。
2 簡單的例子
java中執(zhí)行python 語句
PythonInterpreter interpreter = new PythonInterpreter(); interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); "); interpreter.exec("print days;");
java調(diào)用python的腳本:
PythonInterpreter interpreter = new PythonInterpreter(); interpreter.execfile("script.py");
java調(diào)用python類當(dāng)中的函數(shù)
先在python文件中定一個(gè)python函數(shù)
def pluser(a,b): # print "the result of pluser is %d" % (a+b) return a+b
在java當(dāng)中去調(diào)用:
PythonInterpreter interpreter = new PythonInterpreter(); interpreter.execfile("F:\\machine learning\\machinelearninginaction\\Ch02\\test.py"); PyFunction function = (PyFunction)interpreter.get("pluser",PyFunction.class); PyObject o = function.__call__(new PyInteger(8),new PyInteger(23));
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springboot中不能獲取post請求參數(shù)的解決方法
這篇文章主要介紹了springboot中不能獲取post請求參數(shù)的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06下載遠(yuǎn)程maven倉庫的jar?手動放到本地倉庫詳細(xì)操作
這篇文章主要介紹了如何下載遠(yuǎn)程maven倉庫的jar?手動放到本地倉庫,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03log4j2 項(xiàng)目日志組件的實(shí)例代碼
下面小編就為大家分享一篇log4j2 項(xiàng)目日志組件的實(shí)例代碼,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12SpringBoot利用切面注解及反射實(shí)現(xiàn)事件監(jiān)聽功能
這篇文章主要介紹了springboot事件監(jiān)聽,通過利用切面、注解、反射實(shí)現(xiàn),接下來將對這幾種方式逐一說明,具有很好的參考價(jià)值,希望對大家有所幫助2022-07-07