在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腳本
#當我們的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)容互相對應)
這種方式我們需要使用輸入流輸出流BufferedReader來截取結(jié)果
總結(jié)
到此這篇關于在java中調(diào)用python腳本的三種方法的文章就介紹到這了,更多相關java調(diào)用python腳本內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot?整合mybatis+mybatis-plus的詳細步驟
這篇文章主要介紹了SpringBoot?整合mybatis+mybatis-plus的步驟,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06
SpringCloud Stream消息驅(qū)動實例詳解
這篇文章主要介紹了SpringCloud Stream消息驅(qū)動的相關知識,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
SpringBoot熱部署Springloaded實現(xiàn)過程解析
這篇文章主要介紹了SpringBoot熱部署Springloaded實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-03-03

