在java中調(diào)用python腳本的三種方法
前言
推薦使用第三種方法,因為只有第三種方法使用Runtime.getRuntime()
才能執(zhí)行含有第三方庫(numpy,matlab,pandas等庫)的python腳本。
方法一:在java程序中執(zhí)行Python語句
1.首先在maven中添加依賴
<dependency> <groupId>org.python</groupId> <artifactId>jython-standalone</artifactId> <version>2.7.0</version> </dependency>
2.使用Jpython中的PythonInterpreter執(zhí)行Python語句
public class Tool{ public static void main(String [] args){ PythonInterpreter interpreter = new PythonInterpreter(); interpreter.exec("print("This is a JPython text")"); interpreter.exec("print(2+3)"); } }
方法二:java執(zhí)行python腳本(不支持第三方庫)
1.首先在maven中添加依賴(也是依賴Jpython包)
<dependency> <groupId>org.python</groupId> <artifactId>jython-standalone</artifactId> <version>2.7.0</version> </dependency>
2.創(chuàng)建一個Python腳本
#當(dāng)我們的Python腳本中有漢字的時候,需要在腳本的第一行寫coding = utf-8 來告訴編譯器編碼方式是什么 # -*- coding: UTF-8 -*- a = 'This is a test' print(a)
3.在java中執(zhí)行python腳本
public class Tool{ public static void main(String [] args){ PythonInterpreter interpreter = new PythonInterpreter(); interpreter.execfile("./text.py");//放腳本的位置 interpreter.cleanup(); interpreter.close(); } }
方法三:使用Runtime.getRuntime()執(zhí)行Python腳本
1.不需要傳遞參數(shù)的例子
先創(chuàng)建一個簡單的調(diào)用第三方庫的Python腳本
import numpy as np a = np.arange(10) print(a)
然后使用 Runtime.getRuntime() 方法執(zhí)行python腳本
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Tool { public static void main(String[] args) { try { Process proc = Runtime.getRuntime().exec("test.py");//執(zhí)行腳本 //用輸入輸出流來截取結(jié)果 BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream())); String line = null; while((line = reader.readLine()) != null){ System.out.println(line); } reader.close(); proc.waitFor(); } catch (IOException e) { throw new RuntimeException(e); } catch (InterruptedException e) { throw new RuntimeException(e); } } }
2.需要傳遞參數(shù)的例子
import sys def sum(a, b, c): return a+b+c if __name__ == "__main__": a=(int(sys.argv[1])) b=(int(sys.argv[2])) c=(int(sys.argv[3])) s=sum(a,b,c) print("finish!!!") print(s)
sys.argv用于獲取參數(shù)url1,url2,乃至更多,sys.argv[0]代表python程序名
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; public class Tool { public static void main(String [] args) { Scanner scanner = new Scanner(System.in); int a = scanner.nextInt(); int b = scanner.nextInt(); int c = scanner.nextInt(); try { String[] my_args =new String[] {"python","test.py",String.valueOf(a),String.valueOf(b),String.valueOf(c)}; Process proc = Runtime.getRuntime().exec(my_args);//執(zhí)行腳本 BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream())); String line = null; while((line = reader.readLine()) != null){ System.out.println(line); } reader.close(); proc.waitFor(); } catch (IOException e) { throw new RuntimeException(e); } catch (InterruptedException e) { throw new RuntimeException(e); } } }
my_args數(shù)組里面存放的參數(shù){“python”,“test.py”,String.valueOf(a),String.valueOf(b),String.valueOf©},第一個是固定的就寫’python‘,第二個是我們要執(zhí)行的python腳本的位置(注意路徑),后面的是我們要傳遞的參數(shù)也就是url1,url2等等(和Python腳本所接收的內(nèi)容互相對應(yīng))
這種方式我們需要使用輸入流輸出流BufferedReader來截取結(jié)果
總結(jié)
到此這篇關(guān)于在java中調(diào)用python腳本的三種方法的文章就介紹到這了,更多相關(guān)java調(diào)用python腳本內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mac使用Idea配置傳統(tǒng)SSM項目(非maven項目)
本文主要介紹了Mac使用Idea配置傳統(tǒng)SSM項目(非maven項目),將展示如何設(shè)置項目結(jié)構(gòu)、添加依賴關(guān)系等,具有一定的參考價值,感興趣的可以了解一下2024-01-01spring/springboot整合curator遇到的坑及解決
這篇文章主要介紹了spring/springboot整合curator遇到的坑及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05Java Web Listener實(shí)現(xiàn)事件監(jiān)聽與處理
Java Web開發(fā)中的Listener是一種事件機(jī)制,通過監(jiān)聽Web應(yīng)用程序的事件,實(shí)現(xiàn)對事件的處理,從而實(shí)現(xiàn)更加靈活和高效的應(yīng)用程序開發(fā)。Listener能夠監(jiān)聽的事件包括應(yīng)用程序啟動和關(guān)閉、Session創(chuàng)建和銷毀、請求和響應(yīng)對象的創(chuàng)建和銷毀等2023-04-04SpringBoot報錯Invalid?bound?statement?(not?found)問題排查和解決方案
這篇文章主要介紹了SpringBoot報錯Invalid?bound?statement?(not?found)問題排查和解決方案,文中通過圖文結(jié)合的方式講解的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-03-03