Java執(zhí)行Python代碼的五種場景與示例方法
1.為什么
python擁有的某些庫要比Java強大,也擁有一些比Java更擅長的領域,python可以搭建后端讓Java調(diào)用接口,但某些時候我們用到的python代碼可能并不多也許只有一個算法,此時就需要以下方法了。
2.核心依賴
毫無疑問【自然是python的Java執(zhí)行器了】
<dependency> <groupId>org.python</groupId> <artifactId>jython-standalone</artifactId> <version>2.7.0</version> </dependency>
3.使用
3.1類型一【直接執(zhí)行python代碼】
public class ExecPythonCode {
public static void main(String[] args) {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("a=[5,2,3,9,4,0];");
// 此處python語句是3.x版本的語法
interpreter.exec("print(sorted(a));");
// 此處是python語句是2.x版本的語法
interpreter.exec("print sorted(a);");
interpreter.close();
}
}
3.2類型二【執(zhí)行python文件后獲取返回結果】
1.無參數(shù)的python文件執(zhí)行
public class ExecPythonFile {
public static void main(String[] args) {
try {
Process process = Runtime.getRuntime()
.exec("python D:\\PythonFile.py");
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
process.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
2.帶參數(shù)的python文件執(zhí)行
public class ExecPythonFileWithArgs {
public static void main(String[] args) {
int a = 18, b = 19;
args = new String[] { "python","D:\\PythonFileWithArgs.py",
String.valueOf(a), String.valueOf(b) };
try {
Process process = Runtime.getRuntime().exec(args);
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
process.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
3.【W(wǎng)indows環(huán)境】使用bat腳本執(zhí)行python文件【我猜想也是有Linux環(huán)境的執(zhí)行方法的】
public class ExecPythonBat {
public static void main(String[] args) {
try {
Process process = Runtime.getRuntime().exec("cmd /c D:\\RunPythonFile.bat");
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
process.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
3.3類型三【讀取python文件內(nèi)的函數(shù)進行執(zhí)行】
public class ExecPythonFileCode {
public static void main(String[] args) {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("D:\\PythonFile.py");
PyFunction function = interpreter.get("add", PyFunction.class);
int a = 3, b = 12;
PyObject pyObject = function.__call__(new PyInteger(a), new PyInteger(b));
System.out.println("The result is : " + pyObject);
interpreter.close();
}
}
4.python文件和執(zhí)行腳本
文件一:PythonFile.py
import numpy as np
a = np.arange(12).reshape(3,4)
print(a)
def add(a,b):
return a+b;
文件二:PythonFileWithArgs.py
import sys
def func(a,b):
return (a+b)
if __name__ == '__main__':
a = []
for i in range(1, len(sys.argv)):
a.append((int(sys.argv[i])))
print(func(a[0],a[1]))
文件三:RunPythonFile.bat
@echo off cmd /k python E:\Anaconda3_Python\PythonFile.py
到此這篇關于Java執(zhí)行Python代碼的五種場景與示例方法的文章就介紹到這了,更多相關Java執(zhí)行Python代碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring boot實現(xiàn)數(shù)據(jù)庫讀寫分離的方法
本篇文章主要介紹了Spring boot實現(xiàn)數(shù)據(jù)庫讀寫分離的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01
SpringBoot實現(xiàn)使用反射模擬IOC和getBean
這篇文章主要介紹了SpringBoot實現(xiàn)使用反射模擬IOC和getBean,IOC就是spring的核心思想之一——控制反轉。這里不再贅述,看此文章即可了解2023-04-04
SpringBoot利用Validation包實現(xiàn)高效參數(shù)校驗
如果不進行校驗就直接使用這些數(shù)據(jù),可能會導致各種問題,那么SpringBoot如何利用Validation包實現(xiàn)高效參數(shù)校驗呢,下面讓我們一起來探討這個重要的話題吧2025-04-04
springboot基于docsify?實現(xiàn)隨身文檔
這篇文章主要介紹了springboot基于docsify實現(xiàn)隨身文檔的相關資料,需要的朋友可以參考下2022-09-09
springcloud之Feign、ribbon如何設置超時時間和重試機制
這篇文章主要介紹了springcloud之Feign、ribbon如何設置超時時間和重試機制,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08

