Java 調用 Python 方法是一種常用的跨語言調用技術,在一些場景下具有很大的優勢。本文將從多個方面闡述 Java 調用 Python 方法的實現方式和技巧。
一、Jython庫的使用
Jython是指用Java語言實現的Python解釋器,可以讓Java程序調用Python腳本,同時也能夠讓Python程序調用Java類和方法。使用Jython來調用Python方法非常簡單,只需要按照以下步驟操作即可:
- 通過Jython庫,導入Python的方法或類。
- 通過Java程序,實例化Python方法或類。
- 通過Java程序,調用Python方法或類的方法。
下面是代碼示例:
import org.python.core.PyFunction; import org.python.core.PyObject; import org.python.core.PySystemState; import org.python.util.PythonInterpreter; public class TestJython { public static void main(String[] args) { PythonInterpreter interpreter = new PythonInterpreter(); PySystemState sys = Py.getSystemState(); sys.path.add("D:\\test"); interpreter.exec("from test import add"); PyFunction pyFunction = interpreter.get("add", PyFunction.class); int a = 1, b = 2; PyObject pyobj = pyFunction.__call__(new PyInteger(a), new PyInteger(b)); System.out.println(pyobj.toString()); } }
二、使用Runtime.getRuntime().exec()方法調用Python腳本
使用Java自帶的Runtime.getRuntime().exec()方法調用Python腳本也很簡單。只需要在Java程序中執行Python腳本的命令即可。但需要注意的是,在調用Python腳本之前,需要先切換到Python腳本所在的目錄下。下面是代碼示例:
try { Process process = Runtime.getRuntime().exec("python test.py", null, new File("D:\\test\\")); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = bufferedReader.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); }
三、使用Apache Commons Exec調用Python腳本
Apache Commons Exec是Apache的一個子項目,用於在Java程序中執行外部進程,包括Python解釋器。使用Apache Commons Exec調用Python腳本相對於使用Java自帶的Runtime.getRuntime().exec()方法更加方便,可以更加靈活地操作進程執行過程,並發現進程執行過程中的錯誤。下面是代碼示例:
CommandLine commandLine = CommandLine.parse("python test.py"); DefaultExecutor executor = new DefaultExecutor(); executor.setWorkingDirectory(new File("D:\\test\\")); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); executor.setStreamHandler(streamHandler); int result = executor.execute(commandLine); System.out.println(outputStream.toString());
四、在Java中使用Python第三方庫
在Java中使用Python第三方庫也是一個非常常見的需求。Python有豐富的第三方庫,可以提供各種各樣的功能,比如數據分析、機器學習等等。使用Java調用Python第三方庫的步驟如下:
- 在Java中安裝Python庫,可以使用pip來安裝。
- 在Java程序中導入Python庫,可以使用Jython、Py4J等工具導入。
- 在Java程序中調用Python庫的方法。
下面是代碼示例:
PythonInterpreter interpreter = new PythonInterpreter(); interpreter.exec("import numpy as np"); interpreter.exec("arr = np.array([1, 2, 3])"); interpreter.exec("print(arr)");
五、使用Py4J庫調用Python方法
Py4J是一種Python和Java之間互操作的輕量級框架。使用Py4J可以在Java程序中調用Python代碼,也可以在Python程序中調用Java代碼。使用Py4J調用Python方法需要按照以下步驟操作:
- 在Java程序中啟動Py4J Gateway。
- 在Python程序中啟動Py4J Client。
- 在Java程序中,通過Gateway訪問Python中的對象或方法。
下面是代碼示例:
public class TestPy4J { public static void main(String[] args) { GatewayServer gatewayServer = new GatewayServer(new TestPy4J()); gatewayServer.start(); PythonInterpreter interpreter = new PythonInterpreter(); interpreter.exec("from py4j.java_gateway import JavaGateway"); interpreter.exec("gateway = JavaGateway()"); interpreter.exec("test = gateway.jvm.Test()"); interpreter.exec("test.sayHello()"); gatewayServer.shutdown(); } public void sayHello() { System.out.println("Hello Py4J!"); } } public class Test { public void sayHello() { System.out.println("Hello Py4J!"); } }
總結
本文主要介紹了Java調用Python方法的多種方式,包括Jython、Runtime.getRuntime().exec()、Apache Commons Exec、Py4J等。針對每一種方式,都提供了相應的代碼示例和詳細的講解。希望讀者可以從本文中獲得實際的幫助和啟發。
原創文章,作者:UYPNT,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/375301.html