Java執(zhí)行Python代碼的五種場景與示例方法
1.為什么
python擁有的某些庫要比Java強大,也擁有一些比Java更擅長的領域,python可以搭建后端讓Java調用接口,但某些時候我們用到的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.【Windows環(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文件內的函數(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代碼內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Android Studio更改項目使用的JDK(詳細步驟)
本文介紹了如何在Android Studio中修改Gradle和JDK的配置步驟,包括打開設置、進入Gradle設置、修改JDK路徑、保存并生效等,感興趣的朋友跟隨小編一起看看吧2024-11-11
SpringBoot 引?MybatisGenerator的實現(xiàn)步驟
本文主要介紹了SpringBoot 引?MybatisGenerator的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2025-02-02
spring中@Configuration和@Bean注解的用法
這篇文章主要介紹了spring中@Configuration和@Bean注解的用法,@Configuration用于定義配置類,可替換xml配置文件,被注解的類內部包含有一個或多個被@Bean注解的方法,需要的朋友可以參考下2023-05-05
Spring Boot 3.x 全新的熱部署配置方式詳解(IntelliJ ID
這篇文章主要介紹了Spring Boot 3.x 全新的熱部署配置方式(IntelliJ IDEA 2023.1),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07

