首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Java 調用 Python 的三種實用方式總結

     編輯:Java 相關知識

前言

跨語言調用常見於複用 Python 科學計算庫、NLP 模型、腳本邏輯。本文介紹 Jython 為基礎的三種調用方式(需引入 jython-standalone 依賴)。

依賴配置

<dependency>
    <groupId>org.python</groupId>
    <artifactId>jython-standalone</artifactId>
    <version>2.7.3</version> <!-- 或最新版 -->
</dependency>

方式一:直接執行 Python 語句

import org.python.util.PythonInterpreter;

public class DirectExec {
    public static void main(String[] args) {
        PythonInterpreter interp = new PythonInterpreter();
        interp.exec("days = ('Mon','Tue','Wed','Thu','Fri','Sat','Sun')");
        interp.exec("print(days[1])"); // 輸出:Tue
    }
}

適用於簡單邏輯、動態腳本執行。

方式二:調用 .py 檔案中的函數

my_utils.py

def adder(a, b):
    return a + b

Java 調用

import org.python.core.*;
import org.python.util.PythonInterpreter;

public class CallFunction {
    public static void main(String[] args) {
        PythonInterpreter interp = new PythonInterpreter();
        interp.execfile("C:/Python27/programs/my_utils.py"); // 載入腳本
        
        PyFunction func = (PyFunction) interp.get("adder", PyFunction.class);
        PyObject result = func.__call__(new PyInteger(2010), new PyInteger(2));
        System.out.println("answer = " + result); // 輸出:2012
    }
}

關鍵:interp.get("函數名", PyFunction.class) 獲取函數物件,__call__ 傳遞 PyObject 參數。

方式三:直接執行 .py 檔案腳本

input.py

print('hello')
numbers = [3,5,2,0,6]
print(numbers)
numbers.sort()
print(numbers)
numbers.append(0)
print(numbers)
print(numbers.count(0))
print(numbers.index(5))

Java 執行

public class ExecScript {
    public static void main(String[] args) {
        PythonInterpreter interp = new PythonInterpreter();
        interp.execfile("C:/Python27/programs/input.py");
        /* 控制台輸出:
        hello
        [3, 5, 2, 0, 6]
        [0, 2, 3, 5, 6]
        [0, 2, 3, 5, 6, 0]
        2
        3
        */
    }
}

進階話題與替代方案

方案優點缺點
Jython (本文)純 Java、無外部進程、共享記憶體僅支援 Python 2.7 語法、效能較 CPython 低、無法用 C 擴展庫
ProcessBuilder 執行 python3支援完整 Python 3 生態、C 擴展進程開銷大、序列化傳參複雜、延遲高
gRPC / REST / ZeroMQ 微服務語言無關、獨立部署、擴展性強架構複雜、網路延遲、運維成本高
GraalVM Polyglot API同一 JVM 運行多語言、高效能技術較新、學習曲線陡峭

選型建議

  1. 簡單邏輯、Python 2.7 兼容 → Jython 直嵌。
  2. 需 NumPy/Pandas/TensorFlow 等 C 擴展 → 微服務或進程調用。
  3. 高效能、多語言共存 → GraalVM。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved