一般的場景可以通過使用 Runtime.getRuntime().exec() 來完成,該命令返回一個 Process 對象實例。
...
String[] cmdArgs = {"Rscript", medreportPath, "-i", destFilePath1, "-o", webRptPath + "/" + runID}; // String command = "Rscript " + medreportPath + " -i " + destFilePath1 + " -o " + webRptPath + "/" + runID; try { Process process = Runtime.getRuntime().exec(cmdArgs); // 調動外部程序 BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8)); // 獲取標准輸出 String line; while ((line = br.readLine()) != null) { System.out.println(line + "\n"); } BufferedReader brError = new BufferedReader(new InputStreamReader(process.getErrorStream(), StandardCharsets.UTF_8)); // 獲取標准錯誤輸出 String lineError; while((lineError = brError.readLine()) != null) { System.out.println(lineError + "\n"); } process.waitFor(); } catch (Exception e) { e.printStackTrace(); }
...
以 Java 調用 Python 為例
1. 使用 Runtime 類
該方式簡單,但是增加了 Java 對python 的依賴,需要事先安裝python,及python程序依賴的第三方庫
Runtime 使用了單例模式,只能使用 Runtime 的 static 方法獲取實例
可以調用 exec() 來執行外部程序,返回 Process 對象實例
public class Runtime extends Object // Runtime 直接繼承 Object public static Runtime getRunTime() // 獲取 Runtime 實例的 static 方法 public Process exec(...) throws IOException // 執行外部程序,exec()有多個重載方法
例子
Java 程序代碼
package java_python; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; /* * 直接使用 Runtime 調用外部程序 * 在Java中通過Runtime調用Python程序與直接執行Python程序的效果是一樣的,可以在Python中讀取傳遞的參數,也可以在Java中讀取到Python的執行結果。 * 需要注意的是,不能在Python中通過return語句返回結果,只能將返回值寫入到標准輸出流中,然后在Java中通過標准輸入流讀取Python的輸出值。 */ public class RunPythonByRuntime { public static void main(String[] args) { String exe = "python"; // 使用python3 依賴biopython String command = "C:\\Users\\mail.simcere.com\\eclipse-workspace\\python_test\\test\\test01.py"; // python 腳本路徑 String term = "meningitis"; // 參數 String[] cmdArr = new String[] {exe, command, term}; Process process; try { process = Runtime.getRuntime().exec(cmdArr); // 執行python程序 InputStream inputStream = process.getInputStream(); // 獲取python程序的標准輸出 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); // 轉換為BufferedReader對象實例 String line = null; while((line = bufferedReader.readLine()) != null) { // 逐行讀入python程序的輸出結果 System.out.println(line); } process.waitFor(); } catch (Exception e) { e.printStackTrace(); } } }
Python 程序代碼
#coding=utf-8 import sys from Bio import Entrez from Bio import Medline term = sys.argv[1] # 需要外界傳入 meningitis retmax = 5 # 控制返回的文獻數目 Entrez.email = "771966294@qq.com" handle = Entrez.esearch(db="pubmed", term=term, retmax=retmax) record = Entrez.read(handle) handle = Entrez.efetch(db="pubmed", id=record["IdList"], rettype="medline", retmode="text") records = Medline.parse(handle) records = list(records) handle.close() rst = "" for record in records: # record 是一個字典 rst += ">\nPMID:" + record.get("PMID", "?") \ + "\nTitle:" + record.get("TI", "?") \ + "\nAuthors:" + ";".join(record.get("AU", "?")) \ + "\nAbstract:" + record.get("AB", "?") \ + "\nKeywords:" + ";".join(record.get("OT", "?")) \ + "\nMesh Terms:" + ";".join(record.get("MH", "?")) \ + "\nJournal:" + record.get("TA", "?") \ + "\nDate of Publication:" + record.get("DP", "?") \ + "\ndoi:" + record.get("SO", "?") \ + "\n" print(rst)
2. 使用 Jython 需要事先安裝 Jython
通過 Jython 可以實現 Java 和 Python 的互相調用,使粒度更加精細,但並沒用解決耦合度的問題。
https://www.cnblogs.com/nuccch/p/8435693.html
