你還在糾結怎么樣在Java中調(diào)用python嗎?我們在實際工程項目問題中,經(jīng)常會碰到不同語言代碼之間互調(diào)的問題,比如此處的Java調(diào)用python(常見Java調(diào)用python寫的處理模型來完成數(shù)據(jù)處理等)。
讓我們來看看具體怎么操作吧!
1. 無參數(shù)調(diào)用
說明: Java調(diào)用不帶參數(shù)的python代碼執(zhí)行
樣例代碼如下:
try {
String exe = "python解釋器所處的絕對路徑";
String py = "python代碼文件絕對地址";
Process process = Runtime.getRuntime().exec(exe + " " + py);
//獲取結果的同時設置輸入流編碼格式"gb2312"
InputStreamReader isr = new InputStreamReader(process.getInputStream(),"gb2312");
LineNumberReader input = new LineNumberReader(isr);
String result = "";
result = input.readLine();
System.out.println(result);
input.close();
isr.close();
process.waitFor();
} catch (InterruptedException | IOException e) {
System.out.println("調(diào)用python腳本并讀取結果時出錯:" + e.getMessage());
}
2. 帶參數(shù)調(diào)用
帶參調(diào)用可以將命令和參數(shù)寫入String數(shù)組,然后作為執(zhí)行參數(shù)執(zhí)行。
基本語句如下:
String exe = "python解釋器所處的絕對路徑";
String py = "python代碼文件絕對地址";
String pram = "單個傳遞參數(shù),若參數(shù)為基本類型,轉化為String;若為數(shù)組等類型,也是將其轉換為String型";
String [] args = new String[] {exe, py, pram...};
Process process = Runtime.getRuntime().exec(args);
2.1. 單行返回值
說明: Java調(diào)用不帶參數(shù)的python代碼執(zhí)行
樣例代碼如下:
try {
String exe = "python解釋器所處的絕對路徑";
String py = "python代碼文件絕對地址";
String pram = "單個傳遞參數(shù),若參數(shù)為基本類型,轉化為String;若為數(shù)組等類型,也是將其轉換為String型";
String [] args = new String[] {exe, py, pram...};
Process process = Runtime.getRuntime().exec(args);
//獲取結果的同時設置輸入流編碼格式"gb2312"
InputStreamReader isr = new InputStreamReader(process.getInputStream(),"gb2312");
LineNumberReader input = new LineNumberReader(isr);
String result = "";
result = input.readLine();
System.out.println(result);
input.close();
isr.close();
process.waitFor();
} catch (InterruptedException | IOException e) {
System.out.println("調(diào)用python腳本并讀取結果時出錯:" + e.getMessage());
}
2.2. 多行返回值
說明: Java調(diào)用不帶參數(shù)的python代碼執(zhí)行
樣例代碼如下:
try {
String exe = "python解釋器所處的絕對路徑";
String py = "python代碼文件絕對地址";
String pram = "單個傳遞參數(shù),若參數(shù)為基本類型,轉化為String;若為數(shù)組等類型,也是將其轉換為String型";
String [] args = new String[] {exe, py, pram...};
ProcessBuilder builder = new ProcessBuilder(args);
Process process = builder.start();
BufferedReader success = new BufferedReader(new InputStreamReader(process.getInputStream(), "GB2312"));//獲取字符輸入流對象
BufferedReader error = new BufferedReader(new InputStreamReader(process.getErrorStream(), "GB2312"));//獲取錯誤信息的字符輸入流對象
String line = null;
List<String> success_result = new ArrayList<>();
List<String> error_result = new ArrayList<>();
//記錄輸出結果
while ((line = success.readLine()) != null) {
success_result.add(line);
}
//記錄錯誤信息
while ((line = error.readLine()) != null) {
error_result.add(line);
}
success.close();
error.close();
process.waitFor();
System.out.println(success_result);
System.out.println(error_result);
} catch (InterruptedException | IOException e) {
System.out.println("調(diào)用python腳本并讀取結果時出錯:" + e.getMessage());
}
3. Java中直接執(zhí)行python語句
注意: 此方法在使用之前需要導入依賴環(huán)境,如在maven中導入如下依賴:
<dependency>
<groupId>org.python</groupId>
<artifactId>jython-standalone</artifactId>
<!--python版本在這里指定(2.x或3.x等)-->
<version>3.7.0</version>
</dependency>
調(diào)用語句如下:
import org.python.util.PythonInterpreter
public class JavaRunPython {
public static void main(String[] args) {
//調(diào)用python的解釋器
PythonInterpreter interpreter = new PythonInterpreter();
//執(zhí)行Python語句
interpreter.exec("str = 'hello world!'; ");
interpreter.exec("print(str);");
}
}
4. 通過PythonInterpreter直接調(diào)用python腳本
注意: 此方法也需要導入1.3中依賴
Java調(diào)用代碼如下:
import org.python.util.PythonInterpreter;
public class JavaPythonFile {
public static void main(String[] args) {
PythonInterpreter interpreter = new PythonInterpreter();
//我在這里使用相對路徑,注意區(qū)分
interpreter.execfile("D:/code/test.py");
}
}
test.py舉例如下:
a = 1
b = 2
print(a +b)
5. Java通過調(diào)用bat文件間接調(diào)用python
hello.bat測試代碼如下:
echo hello world!
D:
cd D:\code\
start python test.py
pause
Java調(diào)用代碼如下:文章來源:http://www.zghlxwxcb.cn/news/detail-488245.html
try {
StringBuilder sb = new StringBuilder();
String batPath = "D:/hello.bat";
Process process = Runtime.getRuntime().exec(batPath);
InputStream in = process.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line + "\n");
}
in.close();
try {
process.waitFor();
} catch (InterruptedException e) {
System.out.println(e);
}
} catch (IOException e) {
System.out.println(e);
}
如果大家有好的方法,歡迎交流評論!文章來源地址http://www.zghlxwxcb.cn/news/detail-488245.html
到了這里,關于Java調(diào)用python代碼的五種方式的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!