Java中如何呼叫Python
Python語言有豐富的系統管理、資料處理、統計類別軟體包,因此從java應用程式呼叫Python程式碼的需求很常見、實用。 DataX 是阿里開源的異質資料來源離線同步工具,致力於實現包含關聯式資料庫(MySQL、Oracle等)、HDFS、Hive、ODPS、HBase、FTP等各種異質資料來源之間穩定且有效率的數據同步功能。 Datax也是透過Java呼叫Python腳本。
Java core
Java提供了有兩種方法,分別為ProcessBuilder API和 JSR-223 Scripting Engine。
使用ProcessBuilder
透過ProcessBuilder建立本機作業系統進程啟動python並執行Python腳本, hello.py腳本簡單輸出「Hello Python!」。需要開發環境已經安裝了python,並設定了環境變數。
@Test public void givenPythonScript_whenPythonProcessInvoked_thenSuccess() throws Exception { ProcessBuilder processBuilder = new ProcessBuilder("python", resolvePythonScriptPath("hello.py")); processBuilder.redirectErrorStream(true); Process process = processBuilder.start(); List<String> results = readProcessOutput(process.getInputStream()); assertThat("Results should not be empty", results, is(not(empty()))); assertThat("Results should contain output of script: ", results, hasItem(containsString("Hello Python!"))); int exitCode = process.waitFor(); assertEquals("No errors should be detected", 0, exitCode); } private List<String> readProcessOutput(InputStream inputStream) throws IOException { try (BufferedReader output = new BufferedReader(new InputStreamReader(inputStream))) { return output.lines() .collect(Collectors.toList()); } } private String resolvePythonScriptPath(String filename) { File file = new File("src/test/resources/" + filename); return file.getAbsolutePath(); }
要重寫這句話可這樣說: 使用帶有一個參數的Python指令來啟動,該參數是Python腳本的完整路徑。可以放在java工程的resources目錄下。要注意的是:redirectErrorStream(true),為了使得當執行腳本出現錯誤時,錯誤輸出流會合併到標準輸出流。可以透過呼叫Process物件的getInputStream()方法來讀取錯誤訊息。如果沒有該設置,則需要分別用兩個方法取得流:getInputStream() 和 getErrorStream() 。從ProcessBuilder中取得Process物件後,透過讀取輸出流來驗證結果。
使用Java腳本引擎
JSR-223規格是Java 6首次引入的,該規格定義了一組腳本API,可以提供基本腳本功能。這些API提供了在Java和腳本語言之間共享值及執行腳本的機制。這個規格主要目的是為了統一Java與不同實作JVM的動態腳本語言的交互,Jython是在jvm上執行python的java實作。假設我們在CLASSPATH上有Jython,框架自動發現我們有可能使用該腳本引擎,並允許我們直接請求Python腳本引擎。在Maven中,我們可以引用Jython,也可以直接下載安裝它
<dependency> <groupId>org.python</groupId> <artifactId>jython</artifactId> <version>2.7.2</version> </dependency>
可以透過下面程式碼列出所有支援的腳本引擎:
public static void listEngines() { ScriptEngineManager manager = new ScriptEngineManager(); List<ScriptEngineFactory> engines = manager.getEngineFactories(); for (ScriptEngineFactory engine : engines) { LOGGER.info("Engine name: {}", engine.getEngineName()); LOGGER.info("Version: {}", engine.getEngineVersion()); LOGGER.info("Language: {}", engine.getLanguageName()); LOGGER.info("Short Names:"); for (String names : engine.getNames()) { LOGGER.info(names); } } }
如果Jython在環境中可用,應該要看到對應的輸出:
...
Engine name: jython
Version: 2.7.2
Language: python
Short Names:
python
jython
現在使用Jython呼叫hello.py腳本:
@Test public void givenPythonScriptEngineIsAvailable_whenScriptInvoked_thenOutputDisplayed() throws Exception { StringWriter writer = new StringWriter(); ScriptContext context = new SimpleScriptContext(); context.setWriter(writer); ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("python"); engine.eval(new FileReader(resolvePythonScriptPath("hello.py")), context); assertEquals("Should contain script output: ", "Hello Python!", writer.toString().trim()); }
使用該API比上面的範例更簡潔。在設定ScriptContext時,需要將StringWriter包含其中,以便儲存腳本執行的輸出。然後提供簡稱讓ScriptEngineManager 尋找腳本引擎,可以使用python或jython。最後驗證輸出是否與期望一致。
其實也可以使用PythonInterpretor 類別直接呼叫嵌入的python程式碼:
@Test public void givenPythonInterpreter_whenPrintExecuted_thenOutputDisplayed() { try (PythonInterpreter pyInterp = new PythonInterpreter()) { StringWriter output = new StringWriter(); pyInterp.setOut(output); pyInterp.exec("print('Hello Python!')"); assertEquals("Should contain script output: ", "Hello Python!", output.toString().trim()); } }
PythonInterpreter類別提供的exec方法可直接執行Python程式碼。和前面範例一樣透過StringWriter 捕獲執行輸出。下面再看一個範例:
@Test public void givenPythonInterpreter_whenNumbersAdded_thenOutputDisplayed() { try (PythonInterpreter pyInterp = new PythonInterpreter()) { pyInterp.exec("x = 10+10"); PyObject x = pyInterp.get("x"); assertEquals("x: ", 20, x.asInt()); } }
上面範例可以使用get方法存取變數值。下面範例看如何捕獲錯誤:
try (PythonInterpreter pyInterp = new PythonInterpreter()) { pyInterp.exec("import syds"); }
執行上面程式碼會拋出PyException 異常,與在本機執行Python腳本輸出錯誤一樣。
下面有幾點注意事項:
PythonIntepreter 實作了AutoCloseable,最好是與 try-with-resources 一起使用。
PythonIntepreter類別名稱不是表示Python程式碼的解析器,Python程式在Jython運行在jvm中,執行前需要編譯為java位元組碼。
儘管Jython是Java的Python實現,但它可能不包含與本機Python相同的所有子套件。
下面範例展示如何把java變數賦給Python變數:
import org.python.util.PythonInterpreter; import org.python.core.*; class test3{ public static void main(String a[]){ int number1 = 10; int number2 = 32; try (PythonInterpreter pyInterp = new PythonInterpreter()) { python.set("number1", new PyInteger(number1)); python.set("number2", new PyInteger(number2)); python.exec("number3 = number1+number2"); PyObject number3 = python.get("number3"); System.out.println("val : "+number3.toString()); } } }
以上是Java中如何呼叫Python的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

PHP主要是過程式編程,但也支持面向對象編程(OOP);Python支持多種範式,包括OOP、函數式和過程式編程。 PHP適合web開發,Python適用於多種應用,如數據分析和機器學習。

PHP適合網頁開發和快速原型開發,Python適用於數據科學和機器學習。 1.PHP用於動態網頁開發,語法簡單,適合快速開發。 2.Python語法簡潔,適用於多領域,庫生態系統強大。

Python更適合初學者,學習曲線平緩,語法簡潔;JavaScript適合前端開發,學習曲線較陡,語法靈活。 1.Python語法直觀,適用於數據科學和後端開發。 2.JavaScript靈活,廣泛用於前端和服務器端編程。

PHP適用於Web開發和內容管理系統,Python適合數據科學、機器學習和自動化腳本。 1.PHP在構建快速、可擴展的網站和應用程序方面表現出色,常用於WordPress等CMS。 2.Python在數據科學和機器學習領域表現卓越,擁有豐富的庫如NumPy和TensorFlow。

PHP起源於1994年,由RasmusLerdorf開發,最初用於跟踪網站訪問者,逐漸演變為服務器端腳本語言,廣泛應用於網頁開發。 Python由GuidovanRossum於1980年代末開發,1991年首次發布,強調代碼可讀性和簡潔性,適用於科學計算、數據分析等領域。

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

VS Code 可用於編寫 Python,並提供許多功能,使其成為開發 Python 應用程序的理想工具。它允許用戶:安裝 Python 擴展,以獲得代碼補全、語法高亮和調試等功能。使用調試器逐步跟踪代碼,查找和修復錯誤。集成 Git,進行版本控制。使用代碼格式化工具,保持代碼一致性。使用 Linting 工具,提前發現潛在問題。

VS Code 擴展存在惡意風險,例如隱藏惡意代碼、利用漏洞、偽裝成合法擴展。識別惡意擴展的方法包括:檢查發布者、閱讀評論、檢查代碼、謹慎安裝。安全措施還包括:安全意識、良好習慣、定期更新和殺毒軟件。
