在java中調(diào)用python腳本的三種方法
前言
推薦使用第三種方法,因?yàn)橹挥械谌N方法使用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)文章
簡單講解Android開發(fā)中觸摸和點(diǎn)擊事件的相關(guān)編程方法
這篇文章主要介紹了Android開發(fā)中觸摸和點(diǎn)擊事件的相關(guān)編程方法,包括事件偵聽器等安卓開發(fā)中常用的接口的基本使用方法,需要的朋友可以參考下2015-12-12
java實(shí)現(xiàn)oracle插入當(dāng)前時間的方法
這篇文章主要介紹了java實(shí)現(xiàn)oracle插入當(dāng)前時間的方法,以實(shí)例形式對比分析了java使用Oracle操作時間的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
idea項(xiàng)目的左側(cè)目錄沒了如何設(shè)置
這篇文章主要介紹了idea項(xiàng)目的左側(cè)目錄沒了如何設(shè)置的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
關(guān)于SpringBoot的異?;貪L和事務(wù)的使用詳解
這篇文章主要介紹了關(guān)于SpringBoot的異常回滾和事務(wù)的使用詳解,Spring中 @Transactional 注解,默認(rèn)情況下,只對拋出的RuntimeException 異常,才會事務(wù)回滾,需要的朋友可以參考下2023-05-05
SpringBoot+ MySQL多線程查詢與聯(lián)表查詢性能對比
本文主要介紹了SpringBoot+ MySQL多線程查詢與聯(lián)表查詢性能對比,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-05-05
解決sharding JDBC 不支持批量導(dǎo)入問題
這篇文章主要介紹了解決sharding JDBC 不支持批量導(dǎo)入問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
Java中使用File類創(chuàng)建文件方法總結(jié)
Java File類是I/O操作基礎(chǔ),用于表示文件路徑,提供構(gòu)造方法、文件創(chuàng)建、信息獲取(如名稱、路徑、大?。┘澳夸洸僮?這篇文章主要介紹了Java中使用File類創(chuàng)建文件方法的相關(guān)資料,需要的朋友可以參考下2025-05-05

