詳解如何在Java中調(diào)用Python程序
Java中調(diào)用Python程序
1.新建一個(gè)Maven工程,導(dǎo)入如下依賴
<dependency> <groupId>org.python</groupId> <artifactId>jython-standalone</artifactId> <version>2.7.0</version> </dependency>
2.在java中直接執(zhí)行python代碼片段
import org.python.util.PythonInterpreter; public class InvokePython { public static void main(String[] args) { PythonInterpreter pythonInterpreter = new PythonInterpreter(); pythonInterpreter.exec("a='aaa'"); pythonInterpreter.exec("print(a)"); // pythonInterpreter.exec("import pandas as pd"); } }
通過(guò)上面這種方式執(zhí)行python代碼片段,實(shí)際上是通過(guò)Jpython來(lái)實(shí)現(xiàn)的,這種方式能執(zhí)行的python代碼片段比較有限,都是一些最原始的python命令,很多包不能用,例如執(zhí)行pythonInterpreter.exec("import pandas as pd");
都會(huì)報(bào)錯(cuò)。所以這種方式一般不推薦
3.通過(guò)PythonInterpreter類中的execfile()方法來(lái)執(zhí)行一個(gè)python腳本文件。
import org.python.util.PythonInterpreter; public class InvokePython { public static void main(String[] args) { PythonInterpreter pythonInterpreter = new PythonInterpreter(); pythonInterpreter.execfile("F:\\大學(xué)\\大三\\大三下\\工程創(chuàng)新和企業(yè)開發(fā)\\大作業(yè)\\圖靈API.py"); } }
這種方式和上面的那種方式的本質(zhì)是一樣的,能執(zhí)行的命令也是很原始的,一般不推薦。
4.通過(guò)Runtime.getRuntime().exec()方法來(lái)執(zhí)行python腳本
原python腳本
import requests import json import sys def chat_by_Turing(question): url = "http://www.tuling123.com/openapi/api?key=49de46c409c047d19b2ed2285e8775a6&info=" response = requests.get(url+question) result = json.loads(response.text) answer = result['text'] print("小安:",answer) question = sys.argv[1] ##這個(gè)是用來(lái)接收外部傳進(jìn)來(lái)的參數(shù) chat_by_Turing(question)
Runtime.getRuntime().exec()調(diào)用
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; public class RuntimeFunction { public static void main(String[] args) { Process proc; String compiler = "E:\\Anaconda\\Anaconda_install\\python.exe"; // String program = "F:\\大學(xué)\\大三\\大三下\\工程創(chuàng)新和企業(yè)開發(fā)\\大作業(yè)\\圖靈API.py"; String rootPath = "F:\\大學(xué)\\大三\\大三下\\機(jī)器學(xué)習(xí)\\課設(shè)\\Python\\src\\main\\resources\\"; String program = "圖靈API.py"; try { Scanner in = new Scanner(System.in); System.out.print("我:"); String question = in.nextLine(); String commond = compiler+" "+rootPath+program+" "+question; proc = Runtime.getRuntime().exec(commond); BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream(),"GBK")); String line = null; while ((line = reader.readLine()) != null) { System.out.println(line); } in.close(); proc.waitFor(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }
Runtime.getRuntime().exec()需要傳入一個(gè)字符串類型的參數(shù)command
,完整語(yǔ)句Runtime.getRuntime().exec(command)。這條語(yǔ)句是通過(guò)自己電腦上的cmd來(lái)執(zhí)行的python程序文件的,因此command的構(gòu)成如下:
python解釋器+" “+python程序文件的絕對(duì)路徑+” "+需要給python程序文件傳入的參數(shù)
注意command中的空格比不可少,python腳本里面需要通過(guò)sys.argv來(lái)接收參數(shù)的
通過(guò)這種方式來(lái)執(zhí)行python程序,完全取決于你前面使用的python編譯器,編譯器環(huán)境里面有的,就一定能夠通過(guò)這種方式調(diào)用。這種方式比較強(qiáng)大,推薦使用這種方式來(lái)執(zhí)行python程序。
到此這篇關(guān)于詳解如何在Java中調(diào)用Python程序的文章就介紹到這了,更多相關(guān)Java中調(diào)用Python程序內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot集成普羅米修斯(Prometheus)的方法
這篇文章主要介紹了springboot集成普羅米修斯(Prometheus)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08Mybatis實(shí)現(xiàn)查詢相冊(cè)數(shù)據(jù)列表流程講解
這篇文章主要介紹了Mybatis實(shí)現(xiàn)查詢相冊(cè)數(shù)據(jù)列表流程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-12-12Spring中@Configuration注解的使用場(chǎng)景
這篇文章主要介紹了Spring中@Configuration注解的使用場(chǎng)景,@Configuration注解是從Spring?3.0版本開始加入的一個(gè)使Spring能夠支持注解驅(qū)動(dòng)開發(fā)的標(biāo)注型注解,主要用于標(biāo)注在類上,需要的朋友可以參考下2023-11-11springboot+idea熱部署的實(shí)現(xiàn)方法(自動(dòng)刷新)
這篇文章主要介紹了springboot+idea熱部署的實(shí)現(xiàn)方法(自動(dòng)刷新),本文分步驟通過(guò)實(shí)例代碼截圖相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05