詳解如何在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"); } }
通過上面這種方式執(zhí)行python代碼片段,實(shí)際上是通過Jpython來實(shí)現(xiàn)的,這種方式能執(zhí)行的python代碼片段比較有限,都是一些最原始的python命令,很多包不能用,例如執(zhí)行pythonInterpreter.exec("import pandas as pd");
都會(huì)報(bào)錯(cuò)。所以這種方式一般不推薦
3.通過PythonInterpreter類中的execfile()方法來執(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.通過Runtime.getRuntime().exec()方法來執(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è)是用來接收外部傳進(jìn)來的參數(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ǔ)句是通過自己電腦上的cmd來執(zhí)行的python程序文件的,因此command的構(gòu)成如下:
python解釋器+" “+python程序文件的絕對(duì)路徑+” "+需要給python程序文件傳入的參數(shù)
注意command中的空格比不可少,python腳本里面需要通過sys.argv來接收參數(shù)的
通過這種方式來執(zhí)行python程序,完全取決于你前面使用的python編譯器,編譯器環(huán)境里面有的,就一定能夠通過這種方式調(diào)用。這種方式比較強(qiáng)大,推薦使用這種方式來執(zhí)行python程序。
到此這篇關(guān)于詳解如何在Java中調(diào)用Python程序的文章就介紹到這了,更多相關(guān)Java中調(diào)用Python程序內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java對(duì)接webservice接口的4種方式總結(jié)
因工作需要和一個(gè)Sap相關(guān)系統(tǒng)以WebService的方式進(jìn)行接口聯(lián)調(diào),之前僅聽過這種技術(shù),但并沒有實(shí)操過,所以將本次開發(fā)進(jìn)行記錄,這篇文章主要給大家介紹了關(guān)于java對(duì)接webservice接口的4種方式,需要的朋友可以參考下2023-10-10Java經(jīng)典排序算法之歸并排序?qū)崿F(xiàn)代碼
這篇文章主要介紹了Java經(jīng)典排序算法之歸并排序?qū)崿F(xiàn)代碼,歸并排序是建立在歸并操作上的一種有效的排序算法,該算法是采用分治法的一個(gè)非常典型的應(yīng)用,將已有序的子序列合并,得到完全有序的序列,需要的朋友可以參考下2023-10-10SpringBoot整合Mybatis-Plus實(shí)現(xiàn)關(guān)聯(lián)查詢
Mybatis-Plus(簡(jiǎn)稱MP)是一個(gè)Mybatis的增強(qiáng)工具,只是在Mybatis的基礎(chǔ)上做了增強(qiáng)卻不做改變,MyBatis-Plus支持所有Mybatis原生的特性,本文給大家介紹了SpringBoot整合Mybatis-Plus實(shí)現(xiàn)關(guān)聯(lián)查詢,需要的朋友可以參考下2024-08-08java 通過cmd 調(diào)用命令啟動(dòng)tomcat的操作
這篇文章主要介紹了java 通過cmd 調(diào)用命令啟動(dòng)tomcat的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11詳解OpenFeign服務(wù)調(diào)用(微服務(wù))
OpenFeign是Spring Cloud在Feign的基礎(chǔ)上支持了SpringMVC的注解,如@RequesMapping等等,這篇文章主要介紹了OpenFeign服務(wù)調(diào)用的相關(guān)知識(shí),需要的朋友可以參考下2022-07-07SpringMVC中控制器返回JSON數(shù)據(jù)的實(shí)現(xiàn)
本文主要介紹了SpringMVC中控制器返回JSON數(shù)據(jù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07